-1

Using a regular expression, I need to find and replace only those elements that occur in a row from 2 to 10 times in the content of the text and replace them with something else.

Text example:

$text = '<p>Text text text</p>
<p><a href="#" class="img" target="_blank"><img src="pic_1.jpg" alt=""></a></p>
<p>Text text text</p>
<p><a href="#" class="img" target="_blank"><img src="pic_2.jpg" alt=""></a></p>
<p><a href="#" class="img" target="_blank"><img src="pic_3.jpg" alt=""></a></p>
<p><a href="#" class="img" target="_blank"><img src="pic_4.jpg" alt=""></a></p>
<p><a href="#" class="img" target="_blank"><img src="pic_5.jpg" alt=""></a></p>
<p>Text text text</p>
<p>Text text text</p>
<p>Text text text</p>
<p><a href="#" class="img" target="_blank"><img src="pic_6.jpg" alt=""></a></p>
<p>Text text text</p>';

My regular example:

$regular = "#(<p><a href=['\"](.+?)['\"] class=['\"]img['\"]([^>]+)><img([^>]+)></a></p>){2,10}#is";

I'll try like this

echo preg_replace_callback($regular, function ($matches) {
    return 'some replace text example';
}, $text);

I expect to receive:

<p>Text text text</p>
<p><a href="#" class="img" target="_blank"><img src="pic_1.jpg" alt=""></a></p>
<p>Text text text</p>
some replace text example
some replace text example
some replace text example
some replace text example
<p>Text text text</p>
<p>Text text text</p>
<p>Text text text</p>
<p><a href="#" class="img" target="_blank"><img src="pic_6.jpg" alt=""></a></p>
<p>Text text text</p>

but it doesn't work. What did I do it wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
qubit3003
  • 27
  • 3
  • 8
    [Don't use regex to parse HTML](https://stackoverflow.com/a/1732454/2453432). Use something like [DOMDocument](https://www.php.net/manual/en/class.domdocument.php) instead. – M. Eriksson Feb 02 '22 at 16:54
  • And how can I implement something like this? I looked through the DOMDocument and didn't find anything about what I needed. Again, I need to select and replace only those elements that occur in a row from 2 to 10 times. Everything else should remain as is. – qubit3003 Feb 02 '22 at 17:44
  • DOMDocument is perfect for that. If you have a question about using DOMDocument, then show your attempt and where it goes wrong. – trincot Feb 02 '22 at 17:53
  • 1
    @qubit3003 "*how can I implement something like this?*" Have `DOMDocument` read and parse the HTML, then use its instance methods to implement the logic your requirements prescribe. There are plenty of resources here on this very site as well as the broader Internet (including the documentation linked above) that can get you going in the right direction. Once you have an attempt, edit your question to include the code you've written as a [mre] along with a succinct explanation of where *specifically* in that attempt you're getting stuck. – esqew Feb 02 '22 at 17:53
  • Why do you want to keep the line with `pic_6.jpg`? – Poul Bak Feb 02 '22 at 18:00
  • @PoulBak because I only need to replace what occurs several times in a row – qubit3003 Feb 02 '22 at 18:05
  • You have `highslide` as the class, that won't match your example. – Poul Bak Feb 02 '22 at 18:06
  • @PoulBak This is a typo when posting the question. There img class – qubit3003 Feb 02 '22 at 18:09

1 Answers1

-1

You can use this regex:

$regular = "#(<p><a href=['\"](.+?)['\"] class=['\"]img['\"]([^>]+)><img([^>]+)></a></p>\r?\n){2,10}#i";

Note I have added \r?\n and removed the s flag.

The lines are always separated by \r?\n so that must be part of the match.

Edit:

Forgot this is Linux, \r must be optional.

Poul Bak
  • 10,450
  • 5
  • 32
  • 57