我正在尝试将所有img
标签包装在我的输出文本中:
$output = '<p>some text <img src=""> some text <img src=""></p>
<p>some text <img src=""> some text <img src=""></p>';
我需要它是:
$output = '<p>some text <figure><img src=""><figcaption>blah blah</figcaption></figure> some text <figure><img src=""><figcaption>blah blah</figcaption></figure></p><p>some text <figure><img src=""><figcaption>blah blah</figcaption></figure> some text <figure><img src=""><figcaption>blah blah</figcaption></figure></p>';
我可以用正则表达式做到这一点吗?
使用 preg_replace()
.在替换中,$0
被替换为正则表达式匹配的任何内容。
$output = preg_replace('/<img[^>]*>/', '<figure>$0<figcaption>blah blah</figcaption></figure>', $output);