0

I would like to replace the string of text contained between opening and closing HTML tags.

This is what I've done to date:

        $content =
                preg_replace(
                    '/\b' . preg_quote( $text, "/" ) . '\b/i',
                    $replace",
                    $content
                );  

The tricky part is that I only wish to replace the text not part of HTML-markup, i.e. not the words in <div class="word"> or <img src="../test.img" alt="word"> -- only the text contained between HTML opening and closing tags.

How might I achieve that objective?

slevy1
  • 3,797
  • 2
  • 27
  • 33
SPQRInc
  • 162
  • 4
  • 23
  • 64
  • 2
    Regular expressions are not capable of that, they are the wrong tool. Use a DOM parser and iterate over the elements. – arkascha Jun 02 '17 at 08:34
  • *That's* why I have this bookmark - I knew there was a reason: https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – CD001 Jun 02 '17 at 08:40
  • No, not a duplicate if you read this question carefully. Yes, regular expressions can do what you seek, but preg_replace() is no longer the right function for this purpose b/c the "\e" param is deprecated. Here is some demo code until this question is re-opened and I can discuss this answer in more detail: https://3v4l.org/7JTnW#output – slevy1 Jun 02 '17 at 19:38

0 Answers0