Link to home
Start Free TrialLog in
Avatar of mms_master
mms_masterFlag for United Kingdom of Great Britain and Northern Ireland

asked on

BB Code Replacement Help

Hi,

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]http://www.google.com[/url]
But I want it to replace BB Code like [url=http://www.google.com]Google[/url]

The code I have so far is:

====================PHP==================
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);
            }
===================PHP===================

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

<a href='http://www.google.com]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
mms_master
ASKER CERTIFIED SOLUTION
Avatar of ryan-h
ryan-h

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mms_master

ASKER

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.
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.
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.

mms_master