Search code examples
phpregexpreg-replacebbcodephpbb3

search diffrent tags and replace in a single preg_replace function


I have this kind of retrieved record from a database where I want to decode formatting values and replace it with appropriate html tag so that it will work fine on display:

[size=150:3a9xfsiy][color=#000080:3a9xfsiy]hello world[/color:3a9xfsiy][/size:3a9xfsiy]

This record was created by phpBB and I am using it to display on other part of the website outside phpBB's control..

What ive tried is to use PREG_REPLACE but question is, is there any way to read different formatted tags as one in regex? example:

[size=150:3a9xfsiy] and [/size:3a9xfsiy] must be searched in a single preg_replace

Solution

  • <?
        $ret = 'I gave my Word to you Word.';
        $pattern = '/\bWord\b/i';
        $ret = preg_replace($pattern,"Heart",$ret);
        var_dump($ret);
    ?>
    

    string(29) "I gave my Heart to you Heart."

    Or if this is what suits you

    You could just use str_replace:

    $str = str_replace(array('<tag>', '</tag>'), array('<newtag>', '</newtag>', $str);