hello, im trying to write a function that converts ubb encoding to html. ubb is a basic syntax that lets users mark up their posts. using something like this is safer than allowing them to use html. there was an article on this a while ago. (using formatted user input)
heres a quick example:
hello -> <b>hello</b>
get the point?
the problem im having is this:
hello ive been looking at regex all day and im going blind!
the pattern im using matches the first and the LAST so the whole sentence becomes bold. also the [/b] and in the middle are left in the post.
here is the pattern:
$string = eregi_replace("([b])(.+)([/b])", "<b>\2</b>", $string);
here is the whole function:
function ubb2html($string)
{
$string = eregi_replace("([b])(.+)([/b])", "<b>\2</b>", $string);
$string = eregi_replace("([u])(.+)([/u])", "<u>\2</u>", $string);
$string = eregi_replace("([i])(.+)([/i])", "<i>\2</i>", $string);
$string = eregi_replace("([p])(.+)([/p])", "<p>\2</p>", $string);
$string = eregi_replace("(\[code\])(.+)(\[/code\])", "<blockquote><pre>\\2</pre></blockquote>", $string);
$string = eregi_replace("(\[quote\])(.+)(\[/quote\])", "<blockquote>\\2</blockquote>", $string);
$string = eregi_replace("(\[list\])(.+)(\[/list\])", "<ul>\\2</ul>", $string);
$string = eregi_replace("(\[list=)(a|1)(\])(.+)(\[/list\])", "<ol type=\\2>\\4", $string);
$string = eregi_replace("\[\*\]", "<li>", $string);
$string = eregi_replace("(^|[[:space:]])(http://[[:graph:]]+)", "<a href=\"\\2\">\\2</a>", $string);
$string = eregi_replace("(^|[[:space:]])(www\\.[[:graph:]]+)", "<a href=\"http://\\2\">\\2</a>", $string);
$string = eregi_replace("(\[url\])(http|https|ftp)(://[[:graph:]]+)(\[/url\])",
"<a href=\"\\2\\3\" target=\"_blank\">\\2\\3</A>", $string);
$string = eregi_replace("(\[url\])([[:graph:]]+)(\[/url\])",
"<a href=\"http://\\2\" target=\"_blank\">\\2</A>", $string);
$string = eregi_replace("(\[url=)(http|https|ftp)(://[[:graph:]]+)(\])(.+)(\[/url\])",
"<a href=\"\\2\\3\" target=\"_blank\">\\5</A>", $string);
$string = eregi_replace("(\[url=)([[:graph:]]+)(\])(.+)(\[/url\])",
"<a href=\"http://\\2\" target=\"_blank\">\\4</A>", $string);
$string = eregi_replace("(\[email\])([[:graph:]]+@[[:graph:]]+)(\[/email\])",
"<a href=\"mailto:\\2\">\\2</A>", $string);
$string = eregi_replace("(\[img\])([[:graph:]]+)(\[/img\])",
"<img src=\"\\2\" border=0 alt=\"\\2\">", $string);
return $string;
}
everything else seems to work ok except for a similar url problem that ill deal with later 🙂 any help would be GREATLY appreciated
thanks!
-paul
http://xhawk.net