Skip to content
Advertisement

PHP iterate every node of html string including text nodes splited by other nodes

I trying to change every readable part of given HTML code using DOMDocument and DOMXPath

JavaScript

It gives me

JavaScript

But strings “TEST_1” and “TEST_2” not changed, because of $xpath->evaluate(‘//[count() = 0]’) gives me only nodes without childrens.

  1. How to get all nodes with nodes like “TEST_1” and “TEST_2”?
  2. How to prevent adding <html> and <body> tags to result?

Advertisement

Answer

Unfortunately, I did not find the correct xpath expression. Solved the problem by recursion. This works:

JavaScript

To cut off <body> and <html> tags I found this https://stackoverflow.com/a/38079328/14495402

If you know xpath style solution, please share))

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement