替换所有“foo"使用 REGEX(PHP 代码)在两个 HTML 标签之间 [英] replace all "foo" between two HTML tags using REGEX (PHP code)

查看:41
本文介绍了替换所有“foo"使用 REGEX(PHP 代码)在两个 HTML 标签之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个正则表达式代码,将所有foo"字符串替换为bar",在 html 标签之间 pre></pre>

这是一个例子:

<代码><>等等等等等等 foo 尝试 foo</p><预>foo 尝试 foo 字 foofoo </pre></html>

应该

<代码><>等等等等等等 foo 尝试 foo</p><预>bar try bar word barbar </pre></html>

so ,这意味着标签 pre 之间的所有 foo 都应该替换为 .

我尝试使用此正则表达式模式,但不起作用.

做{$string = preg_replace('/

([^)]*)foo([^)]*

)/U', '\1boo\2', $string, -1,$count);}while($count != 0);回声 $string;

对不起我的英语谢谢,

解决方案

$string = '<html><>等等等等等等 foo 尝试 foo</p><预>foo 尝试 foo 字 foofoo </pre></html>';$string = preg_replace('/foo(?=(?:.(?!

))*<\/pre>)/Um', 'boo', $string);回声 $string;

I want a regex code , to replace all "foo" strings to "bar" , between the html tags pre>< /pre>

here is an example :

< html>
< p> blah blah blah foo try foo< /p>
< pre> foo try foo word foofoo < /pre>
< /html>

shoud be

< html>
< p> blah blah blah foo try foo< /p>
< pre> bar try bar word barbar < /pre>
< /html>

so , that's means all foo between the tags pre should be replaced by .

i tried to use this regex pattern but its not working.

do {
$string = preg_replace('/< pre>([^)]*)foo([^)]*< /pre>)/U', '\1boo\2', $string, -1,$count);
}while($count != 0);
echo $string;

im sorry for my english Thank you,

解决方案

$string = '< html>
< p> blah blah blah foo try foo< /p>
< pre> foo try foo word foofoo < /pre>
< /html>';
$string = preg_replace('/foo(?=(?:.(?!< pre>))*< \/pre>)/Um', 'boo', $string);
echo $string;

这篇关于替换所有“foo"使用 REGEX(PHP 代码)在两个 HTML 标签之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆