views:

39

answers:

1

Hi everybody! Lets say I'm trying to replace every "A" character with a "B", inside two [lol] tags.

For example:

"[lol]It's greatA really isA[/lol]"

will become

"[lol]It's greatB really isB[/lol]"

I was trying to work something myself but it was in vain. The closest i got is this:

preg_replace("%(\[lol\])"."(.*?)([A]+?)(.*?)"."(\[/lol\])%s", "$1$2B$4$5", $haystack);

of course it doesn't work this way...

I'll appreciate any assistance!

thanks!

+1  A: 
echo preg_replace(
    '/(?<=\[lol\]).*?(?=\[\\/lol\])/e',
    'str_replace("A", "B", "\\0")',
    'AAA sdf [lol]It\'s greatA really isA[/lol] AAAA ' .
        'sdfd [lol]It\'s greatA really isA[/lol] AA sf'
);

gives

AAA sdf [lol]It\'s greatB really isB[/lol] AAAA sdfd [lol]It\'s greatB really isB[/lol] AA sf
Artefacto