开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜