【发布时间】:2016-02-01 08:46:27
【问题描述】:
请帮帮我..我有以下字符串
<p>this is text before first image</p>
<p><a href=""><img class="size-full wp-image-2178636" src="image1.jpg" alt="first" /></a> this is first caption</p>
<p>this is text before second image.</p>
<p><a href=""><img src="image2.jpg" alt="second" class="size-full wp-image-2178838" /></a> this is second caption</p>
<p>there may be many more images</p>
我需要上面的字符串格式如下:
<p>this is text before first image</p>
<a href="">
<figure>
<img class="size-full wp-image-2178636" src="image1.jpg" alt="first" />
<figcaption class="newcaption">
<h1>this is first caption</h1>
</figcaption>
</figure>
</a>
<p>this is text before second image.</p>
<a href="">
<figure>
<img class="size-full wp-image-2178636" src="image2.jpg" alt="first" />
<figcaption class="newcaption">
<h1>this is second caption</h1>
</figcaption>
</figure>
</a>
<p>there may be many more images</p>
请帮助我......我们如何通过正则表达式或使用其他方式来做到这一点。我正在使用 PHP。
问候, 萨钦。
【问题讨论】:
-
具体做什么 - 你的问题不清楚
-
嗨 RamRaider 我现在已经完成了代码缩进。希望现在很清楚。
-
Regex 不是解析 HTML/XML 的正确工具,您可以使用 DOMDocument 方法
-
花了我一段时间才看到你想要什么,但是正则表达式不适合用
figure替换你的img节点,可以做到这一点的正则表达式将非常复杂,这似乎是一种浪费HTML 解析的时间可以更快。