Msc Final Project

RegExp escapes ubbcode (php)

Having been tortured by php for already two weeks…The RegExp(Perl) functions between JS and PHP are slightly different: str.match(pattern);—–preg_match($pattern,$str,$arr);…Below escapes ubbcode into html format by PHP RegExp, it’s just like the text editor.

$str="[b]I am bold[/b],
[i]I am italic[/i],
[b]I am the second bold[/b],
[color=red]I am color[/color],
[size=7]I am size[/size],
[align=center]I am center[/align]".'<br>';
$ubbcodes=array('/\[b\](.*?)\[\/b\]/', //Here remember to add ? to avoid greedy mode and use escape code (preg_quote() also helps)
'<font color= "\1">\2</font>',
'<font size= "\1">\2</font>',
'<p align = "\1">\2</p>');
echo preg_replace($ubbcodes,$htmls,$str);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s