PHP code HTML code

Possible duplicate:
Best HTML Parsing Techniques

How can I parse the HTML stored in a PHP variable if it is something like:

<h1>T1</h1>Lorem ipsum.<h1>T2</h1>The quick red fox...<h1>T3</h1>... jumps over the lazy brown FROG! 

I want to get text between the headers , and I understand that regular expressions are not recommended.

+43
html php parsing
Sep 02 '10 at 13:22
source share
1 answer

Use PHP Document Object Model :

 <?php $str = '<h1>T1</h1>Lorem ipsum.<h1>T2</h1>The quick red fox...<h1>T3</h1>... jumps over the lazy brown FROG'; $DOM = new DOMDocument; $DOM->loadHTML($str); //get all H1 $items = $DOM->getElementsByTagName('h1'); //display all H1 text for ($i = 0; $i < $items->length; $i++) echo $items->item($i)->nodeValue . "<br/>"; ?> 

This is output as:

  T1 T2 T3 



[EDIT]: after defining the OP:

If you need content like Lorem ipsum. etc., you can directly use this regular expression:

 <?php $str = '<h1>T1</h1>Lorem ipsum.<h1>T2</h1>The quick red fox...<h1>T3</h1>... jumps over the lazy brown FROG'; echo preg_replace("#<h1.*?>.*?</h1>#", "", $str); ?> 

these outputs:

Lorem ipsum. Fast red fox ...... jumps through a lazy brown frog

+104
Sep 02 '10 at 13:30
source share



All Articles