我需要一个Php-RegEx来查找php-string中的所有图像标签,并替换image-tag对象中的所有引号(如“)”。
$mystring = '<p class="testclass">test tag with p</p><p><img src="test1.jpg" width="100" /></p><p><img src="test2.jpg" width="100" /></p>';
我需要的结果如下:
<p class="testclass">test tag with p</p><p><img src=:quot:test1.jpg:quot: width=:quot:100:quot: /></p><p><img src=:quot:test2.jpg:quot: width=:quot:100:quot: /></p>
我测试了以下代码:
$rep_html = '<p class="testclass">test tag with p</p><p><img src="test1.jpg" width="100" /></p><p><img src="test2.jpg" width="100" /></p>';
$rep_html = preg_replace('#<img ([^"]) />#',":quot:", $rep_html);
和/或:
$rep_html = preg_replace('#<img ([\x22]) />#',":quot:", $rep_html);
...不幸的是,代码无法正常工作。引号未格式化为:quote:。
我不需要Php“domDocument”和“loadHTML”...只有Php RegEx ......
也许有人可以帮助我?
答案 0 :(得分:1)
搜索
<(?!img)[^>]*>[^<]*(*SKIP)(*FAIL)|"
替换为
:quot:
左侧为code generator
的Regex101 Example为您提供了转码的PHP版本代码。
答案 1 :(得分:0)
我测试了以下代码:
$rep_html = '<p class="testclass">test tag with p</p><p><img src="test1.jpg" width="100" /></p><p><img src="test2.jpg" width="100" /></p>';
$rep_html = preg_replace('#<img ([^"]) />#',":quot:", $rep_html);
和/或:
$rep_html = preg_replace('#<img ([\x22]) />#',":quot:", $rep_html);
...不幸的是,代码无法正常工作。引号未格式化为:quote:。
答案 2 :(得分:0)
使用较少的步骤,您可以使用此模式:
\G(?:(?!\A)|[^<]*+(?:<(?!img)[^>]*+>[^<]*+)*+<)[^">]*+\K"
或者您可以将@OnlineCop模式与S修饰符一起使用。