0

I am trying to wrap all img tags in my output text:

$output = '<p>some text <img src=""> some text  <img src=""></p>
           <p>some text <img src=""> some text  <img src=""></p>';

I need it to be:

$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>';

Can I do that with regexp?

web user
  • 137
  • 1
  • 3
  • 11

1 Answers1

0

Use preg_replace(). In the replacement, $0 gets replaced with whatever the regular expression matched.

$output = preg_replace('/<img[^>]*>/', '<figure>$0<figcaption>blah blah</figcaption></figure>', $output);
Barmar
  • 741,623
  • 53
  • 500
  • 612