PHP: replacing a specific char in a range with preg_replace
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\])"."(.*?)([开发者_如何学GoA]+?)(.*?)"."(\[/lol\])%s", "$1$2B$4$5", $haystack);
of course it doesn't work this way...
I'll appreciate any assistance!
thanks!
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
精彩评论