1

There is HTML:

<div id="attachment_21727" class="someclass">
  <img alt="text" src="url" />
  <p class="wp-caption-text">Text</p>
</div>

I need to replace div to another tag. So i need to find this div,find content in it, and replace.

attachment_21727 - relative as attachment_{number}

So i have a function PHP:

$pattern = "/<div id=\"attachment_(.*?)\" (.*?)>(.*?)<\/div>/i";
$replacement = '<figure>$3</figure>';
$content = preg_replace($pattern, $replacement, $content);

But not working...Any ideas?

Zhurka
  • 55
  • 5

1 Answers1

1

You need to change your regex like below:-

#<div id=\"attachment_(.*?)\">(.+?)</div>#s

Working snippet:-

<?php

$content = '<div id="attachment_21727" class="someclass">
  <img alt="text" src="url" />
  <p class="wp-caption-text">Text</p>
</div>';

$pattern = "#<div id=\"attachment_(.*?)\">(.+?)</div>#s";



$replacement = '<figure>$2</figure>';

echo $content = preg_replace($pattern, $replacement, $content);

Output:-https://eval.in/990377

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98