preg_replace: BB code to HTML URL

671 views Asked by At

I have a little script here which replaces BB code with HTML code. Everything works fine but the URLs.

$bbextended = array(
"/\[URL=(.*?)\](.*?)\[\/URL\]/i" => "<a href=\"$1\" title=\"$1\">$2</a>"
);

foreach($bbextended as $match=>$replacement){
$bbtext = preg_replace($match, $replacement, $bbtext);
}

Input

[URL="http://somewebsite.come/something"]Some Website Title[/URL]

Output

<a href=""http://somewebsite.come/something"" title=""http://somewebsite.come/something"">Some Website Title</a>

There are double-quotes, which obviously isn't that good.

I tried

$bbextended = array(
"/\[URL=\"(.*?)\"\](.*?)\[\/URL\]/i" => "<a href=\"$1\" title=\"$1\">$2</a>"
);

in the code but it didn't work. I also tried to leave out the escape sign and quotes around the $1 in the HTML code but it didn't work neither.

Any ideas?

2

There are 2 answers

0
dynamic On

You should use a real parser for this, such as jBB http://jbbcode.com/

0
AudioBubble On

When I

Set the Find string = '/\[URL="(.*?)"\](.*?)\[\/URL\]/i'
and
Set the replace string = '<a href="$1" title="$1">$2</a>'

I get this using simple preg_replace

<a href="http://somewebsite.come/something" title="http://somewebsite.come/something">Some Website Title</a>