BB Code Replacement Help

Posted on 2004-11-19
Last Modified: 2006-11-17

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
Question by:mms_master
    LVL 1

    Accepted Solution

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

    print $href;

    LVL 5

    Author Comment

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

    Author Comment

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

    Author Comment

    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.


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now