View Full Version : Need Help: str_replace() for UBB code

02-28-2009, 11:10 PM
Hi, trying to convert a UBB url into an actual URL with the <a> anchor

text I am trying to convert:

[ u r l ]http://www.website.com[ / u r l ]

code to convert it into HTML using str_replace would be what exactly?

search would be: ' [ u r l ] "', " [ / u r l ]", (without the spaces)

so the replace would be? ...


03-01-2009, 12:06 AM
Well.. I don't know if you can with str_replace, because you'd need to 'catch' the URL, but with regex you could.


$string = 'http://www.url.com.au';

echo preg_replace('#\[url\]([^\[]+)\[/url\]#', '<a href="$1">$1</a>', $string);

Come to think of you could could do..


$string = 'http://www.url.com.au';

$string = str_replace('', '<a href="', $string);
$string = str_replace('', '">Link</a>', $string);

echo $string;

But that's a really generic way and all links will be 'Link'

03-01-2009, 12:26 AM
Hmm.. that doesn't seem to work.. Here is my code:

function bb2html($text)

// first take care of the [ u r l ]http://www.cnn.com[ / u r l ] type of links

preg_replace('#\[ u r l \]([^\[]+)\[/ u r l \]#', '< a h r e f="$1">$1< / a >', $text);

// then take care of all of the other ubb codes
$bbcode = array(
"<", ">",

"[l is t]", " ", "[/li s t]",

"[i m g]", "[/ i mg]",

"[ b ]", "[/ b ]",

"[ u ]", "[/ u ]",

"[ i ]", "[/ i ]",

'[c o lor="', "[/c o lor]",

"[s ize=\"", "[/s ize]",

// '[u rl="', "[/u rl]",

// '[u rl=', "[/u rl]", // url with and without the beginning " quote

"[m ail=\"", "[/m ail]",

"[c ode]", "[/c ode]",

"[q uote]", "[/q uote]",


']'); // url with and without the beginning " quote

$htmlcode = array(
//"&lt;", "&gt;",

"<ul>", "<li>", "</ul>",

"<img src=\"", "\">",

"<b>", "</b>",

"<u>", "</u>",

"<i>", "</i>",

"<span style=\"color:", "</span>",

"<span style=\"font-size:", "</span>",

// '<a href="', "</a>",

// '<a href="', "</a>",

"<a href=\"mailto:", "</a>",

"<code>", "</code>",

"<table width=100% bgcolor=lightgray><tr><td bgcolor=white>", "</td></tr></table>",



$newtext = str_replace($bbcode, $htmlcode, $text);

$newtext = nl2br($newtext);//second pass

return $newtext;


// $mysql SELECT query code goes here, stored in array using mysql_fetch-array...
echo 'text: '.bb2html($row[contentBody]) ;

An example of the data field stored in mySQL is:

Heres a review of the famous fitness Bender Ball [ u r l ]http://www.domain.-c-o-m/tv-products/bender-ball-review[/ u r l ]
// once again I added the spaces in the url tag

The parsed output using the above PHP code is:

Heres a review of the famous fitness Bender Ball [url">h-t-t-p://w-w-w-.domain.c-o-m/tv-products/bender-ball-review[/url">

// once again I added the spaces in the url tag
// I added the hyphens in "http" to prevent an auto parse by the forum

so i am clearly doing something wrong....

03-01-2009, 12:35 AM
Try this..

preg_replace('#\[url\]([^\[]+)\[/url\]#is', '<a href="$1">$1</a>', $text);

That should work.

03-01-2009, 01:35 AM
well that made absolutely no difference

can anyone help please?

Deacon Frost
03-01-2009, 04:14 AM


// The url
$url = "http://google.com";

// What you want displayed for the link
$title = "Google";

// Convert, if you already have this part of the string, just use it
$newurl = "" . $url . "";

// Change the beginning to display
$part1url = str_replace('', '<a href="', $newurl);

// Change the end to display
$part2url = str_replace('', '">' . $title . '</a>', $newurl);

// Put the two togeter
$finalurl = $part1url . "" . $part2url;

// Prints: "Google" with link
echo $finalurl;


That's how I would go about it, at least.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum