Parsing and editing html using DOM Parser

How to edit HTML page using PHP DOM Parser? I have a page on my site that I want to edit. I tried these things...

 foreach($html->find('div[class=doc-metadata]') as $data){  //bunch of HTML codes that contain a link to be edited. 
      $metadata = $data->children();        

        }

The page contains a link that I want to edit the href. Say...

<a href="this/is/random/link">Link to edit</a>

I tried this.

   foreach($html->find('div[class=doc-metadata]') as $data){    

     $link= $data->find('a');      
     $link->href = null; //set to null or change it completely (i think)  

    }

But after that line, I am completely lost. I really don't know how to do it.

Answers


To change an attribute of an element, you can use setAttribute().

$link->setAttribute("target", "_blank");

After doing this, you might need the HTML output of DOM. You can do this by, saveHTML().

print $html->saveHTML();

Need Your Help

Xcode 7: how to set PATH variable?

xcode macos path xcode7 osx-elcapitan

I have a c-code written in Xcode 7, and running on my Mac laptop running El Capitain (OSX 10.11.6). The program calls another program, "bertini", using the following lines of code:

Why is there a border on this table?

html css html-table

On this page, there seems to be a small problem... there's a sliver of white showing.