BB Code Replacement Help


I am making a forum and want to include BB Code in it.
So far it replaces BB Code like [b]text[/b] and [url][/url]
But I want it to replace BB Code like [url=]Google[/url]

The code I have so far is:

if ($Bb_Code_Row['Advanced_Replace'] > "") {
                  $Post_Main_Text = str_replace($Bb_Code_Row['Open_Code'] . ".]", $Bb_Code_Row['Open_Replace'] . "$1" . $Bb_Code_Row['Advanced_Replace'], $Post_Main_Text);
                  $Post_Main_Text = str_replace($Bb_Code_Row['Close_Code'], $Bb_Code_Row['Close_Replace'], $Post_Main_Text);
            } else {
                  $Post_Main_Text = str_replace($Bb_Code_Row['Open_Code'], $Bb_Code_Row['Open_Replace'], $Post_Main_Text);
                  $Post_Main_Text = str_replace($Bb_Code_Row['Close_Code'], $Bb_Code_Row['Close_Replace'], $Post_Main_Text);

The html output for the advanced BB Code (being [url= ] [/url]) is (using Google as an example):

<a href=']Google</a>

If I replace ] with '> it will replace it for all other BB Codes (e.g. [b]text[/b] would become <b'>text</b'>) which obviously would cause problems.

I've thought about doing it with a pattern, but can't work out how.

Can anyone help me please?

Thanks In Advance
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

$bb = "[url=]Google[/url]";
$out = preg_match("/\[url=(.*)\](.*)\[\/url\]/", $bb, $m);
$href = "<a href='".$m[1]."'>".$m[2]."</a>";

print $href;


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mms_masterAuthor Commented:
the only problem with that is it takes the url out of the string and sets it as $href.

I need my code to keep the url in $Post_Main_Text, because otherwise I have to seperate it out to put $href back in the right part of the post.
mms_masterAuthor Commented:
If it isn't possible to keep it in $Post_Main_Text can you tell me how to replace it and then sepereate the string and post the replacementin the right place.

Or if there is another way to do it then could you tell me please?

The strings will be coming from a database and the code I need will be in a while loop.
mms_masterAuthor Commented:
I searched on google, found some code and modified it.

It's similar to what you said but using pred_replace() instead of preg_match().

Thanks anyway for the help and scene as you gave me an idea of what I was looking for i'll give you the points.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.