使用 php 将所有图像包装在输出中



我正在尝试将所有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);

最新更新