PDA

View Full Version : Regular Expressions..


Ee-Ore
Tue 30th Dec '03, 6:54pm
I wonder if someone from the vbulletin team could provide me with the regular expression thats used to turn either [url =http://lala.com]here[/ url] or [url ]http://lala.com[/ url] (without spaces ;)) into a hyperlink.

or a way in which i can take the contents of $pagetext and pass it through one of the functions already available somewhere in the vb code.

Thanks,

Ee-Ore

Ee-Ore
Tue 30th Dec '03, 9:05pm
What i want to do is...

when i pull information from the database - the actual post information has all of the BBCode in it still. What I want to know is if their is a function within the VBulletin files I can use to convert these bbcodes into their equivalent HTML, or if i could get the regular expressions you use to change from BBCode to HTML.

Thanks,

Ee-Ore

CeleronXT
Wed 31st Dec '03, 2:05am
Just for URL's?

My script does it this way.


function do_bbcode($text)
{
$findarray = array();
$replacearray = array();
// [URL]
$findarray['url1'] = '/\[url\]((http|https|news|ftp):\/\/\w+[^\s\[\]]+)\[\/url\]/i';
$replacearray['url1'] = '<a href="\\1" target="_blank">\\1</a>';
// [URL] - no protocol (assumes http://)
$findarray['url1-noprefix'] = '/\[url\](\w+[^\s\[\]]+)\[\/url\]/i';
$replacearray['url1-noprefix'] = '<a href="http://\\1" target="_blank">\\1</a>';
// [URL=XXX]
$findarray['url2'] = '/\[url=((http|https|news|ftp|aim):\/\/\w+[^\s\[\]]+)\](.+?)\[\/url\]/is';
$replacearray['url2'] = '<a href="\\1" target="_blank" title="\\1">\\3</a>';
// [URL=XXX] - no protocol (assumes http://)
$findarray['url2-noprefix'] = '/\[url=(\w+[^\s\[\]]+)\](.+?)\[\/url\]/is';
$replacearray['url2-noprefix'] = '<a href="http://\\1" target="_blank" title="\\1">\\2</a>';
// Do the replace
$text = preg_replace($findarray, $replacearray, $text);
}

If you were to have that, you could just do...

$pagetext = do_bbcode($pagetext);

Of course if you want all the BBcodes (not just URL), you need a more extensive function..

================================================== ================

This function parses even more codes.. (you can tell which from the comments)


function do_bbcode($pagetext)
{
$findarray = array();
$replacearray = array();
// #################### REPLACEMENTS ####################
// [B]
$findarray['bold'] = '/\[b\](.+?)\[\/b\]/is';
$replacearray['bold'] = '<strong>\\1</strong>';
// [I]
$findarray['italics'] = '/\[i\](.+?)\[\/i\]/is';
$replacearray['italics'] = '<em>\\1</em>';
// [U]
$findarray['underline'] = '/\[u\](.+?)\[\/u\]/is';
$replacearray['underline'] = '<span style="text-decoration: underline">\\1</span>';
// [HR]
$findarray['ruler'] = '/\[hr\]/i';
$replacearray['ruler'] = '<hr />';
// [COLOR=XXX]
$findarray['color'] = '/\[color=([\#a-zA-Z0-9]+)\](.+?)\[\/color\]/is';
$replacearray['color'] = '<span style="color: \\1;">\\2</span>';
// [SIZE=XXX]
$findarray['size'] = '/\[size=([\d])\](.+?)\[\/size\]/is';
$replacearray['size'] = '<font size="\\1">\\2</font>';
// [FONT=XXX]
$findarray['font'] = '/\[font=(.+?)\](.+?)\[\/font\]/is';
$replacearray['font'] = '<span style="font-family: \\1;">\\2</span>';
// [BLOCKQUOTE]
$findarray['blockquote'] = '/\[blockquote\](.+?)\[\/blockquote\]/is';
$replacearray['blockquote'] = '<blockquote>\\1</bockquote>';
// [EMAIL]
$findarray['email'] = '/\[email\]([\w\-\.]+@[\w\-]+\.[\w\-\.]+)\[\/email\]/is';
$replacearray['email'] = '<a href="mailto:\\1"><font face="verdana,arial,helvetica" size="2" color="'.$linkcolor.'">\\1</font></a>';
// [EMAIL=XXX]
$findarray['email2'] = '/\[email=([\w\-\.]+@[\w\-]+\.[\w\-\.]+)\](.+?)\[\/email\]/is';
$replacearray['email2'] = '<a href="mailto:\\1"><font face="verdana,arial,helvetica" size="2" color="'.$linkcolor.'">\\2</font></a>';
// [IMG]
$findarray['image'] = '/\[img\]([-_.\/a-zA-Z0-9!&%\#?+,\'=:~]+)\[\/img\]/i';
$replacearray['image'] = '<img src="\\1" border="0" alt="Image" />';
// [URL]
$findarray['url1'] = '/\[url\]((http|https|news|ftp):\/\/\w+[^\s\[\]]+)\[\/url\]/i';
$replacearray['url1'] = '<a href="\\1" target="_blank">\\1</a>';
// [URL] - no protocol (assumes http://)
$findarray['url1-noprefix'] = '/\[url\](\w+[^\s\[\]]+)\[\/url\]/i';
$replacearray['url1-noprefix'] = '<a href="http://\\1" target="_blank">\\1</a>';
// [URL=XXX]
$findarray['url2'] = '/\[url=((http|https|news|ftp|aim):\/\/\w+[^\s\[\]]+)\](.+?)\[\/url\]/is';
$replacearray['url2'] = '<a href="\\1" target="_blank" title="\\1">\\3</a>';
// [URL=XXX] - no protocol (assumes http://)
$findarray['url2-noprefix'] = '/\[url=(\w+[^\s\[\]]+)\](.+?)\[\/url\]/is';
$replacearray['url2-noprefix'] = '<a href="http://\\1" target="_blank" title="\\1">\\2</a>';
$pagetext = preg_replace($findarray, $replacearray, $pagetext);
}

Ee-Ore
Wed 31st Dec '03, 8:48am
I had already managed to code the rest which is why i was asking only for urls :)

/me wonders now if you might also have a function for dealing with smilies ? :)

CeleronXT
Wed 31st Dec '03, 10:11am
Indeed I do. :D If you need one for a smilie that's not on here, I can write one for it.

function do_smilies($newstext)
{
$newstext = preg_replace('/(&[#a-zA-Z0-9]+;)/i','\\1 ',$newstext); // <--add space after htmlspecialchars
$newstext = str_replace(':)','<img src="images/smilies/happy.gif" border="0" alt="Happy" />', $newstext);
$newstext = str_replace(':(','<img src="images/smilies/sad.gif" border="0" alt="Sad" />', $newstext);
$newstext = str_replace(';)','<img src="images/smilies/wink.gif" border="0" alt="Wink" />', $newstext);
$newstext = str_replace(':D','<img src="images/smilies/biggrin.gif" border="0" alt="Big Grin" />', $newstext);
$newstext = str_replace('^_^','<img src="images/smilies/keke.gif" border="0" alt="Keke" />', $newstext);
$newstext = str_replace(array(':P',':p'),'<img src="images/smilies/tongue.gif" border="0" alt="Tongue" />', $newstext);
$newstext = preg_replace('/:angry:/i','<img src="images/smilies/angry.gif" border="0" alt="Angry" />', $newstext);
$newstext = preg_replace('/:confused:/i','<img src="images/smilies/confused.gif" border="0" alt="Confused" />', $newstext);
$newstext = preg_replace('/:rolleyes:/i','<img src="images/smilies/rolleyes.gif" border="0" alt="Roll Eyes" />', $newstext);
$newstext = preg_replace('/(&[#a-zA-Z0-9]+;)( )/i','\\1',$newstext); // <--strip space after htmlspecialchars
return $newstext;
}

Ee-Ore
Thu 1st Jan '04, 2:46pm
Excellent stuff thanks mate - as for adding me im sure I could do that myself with a little bit of thinking and looking at the way its already done :)

Sorry for delayed reply - been across the other side of the country celebrating new year - hope you had a good one :)

Thanks,

Leon