php - Using DOMDocument, is it possible to get all elements that exists within a certain DOM? -


Assume that I have an HTML file with many different elements, suppose I do not already know that it What the HTML looks like

Using PHP's DOMDocument, how can I recycle and modify the all elements? I see that the elementbet tagname gets and the elementbid is found. I want to iterate through all the elements.

For example suppose that HTML looks like this (just one example, I really do not know the structure):

  $ html = '  

I want to be able to do some simple DOM modifications (like JavaScript):

  $ dom = new DOMDocument (); $ Dom- & gt; loadHTML ($ HTML); // The code below clearly does not work, but shows how I want to get the foreach ($ dom-> GetAllElements $ element) {if (! $ Element- & gt; Hexellale ('Potatoes')) {$ element-> AddClass ('potatoes'); } And ($ element-> removeClass ('potato');}} $ Html = $ dom-> Save HTML ();   

Then in this example, The resultant HTML would look like:

  $ html = '
& gt; Lt; / div & gt; ';

How can I be repeated through all the elements and amend them while flying in the forech-loop? I actually have regex

You can pass an asterisk * with which all elements return:

  foreach ($ dom-> getElementsByTagName ('*') as $ element) {}   

From: Name
The tag's local name (non-namespace) special value tag * matches all tags.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -