Question: > 
> **Possible Duplicate:**  
> 
> [How to parse and process HTML with PHP?](https://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-with-php)
> 
> 
> 

Hi there I have scraped a web page

```
  <div class="col blue">
        <img  src="/en/media/Dentalscreenresized.jpg" />
        <h4>This is line i want to scrape</h4>
        <p class="date">12 Sep
            <span class="year">2012</span></p>
        <p>13 people were diagnosed with oral cancer after last year&rsquo;s Mouth Cancer Awareness Day. Ring 021-4901169 to arrange for a free screening on the 19th September.</p>
        <p class="readmore"><a href="/en/news/abcd.html">Read More</a></p>
        <p class="rightreadmore"><a href="http://www.xyz.ie/en/news/">See all News&nbsp;&nbsp;&nbsp;</a></p>
    </div>

```

Now i want to display the `<h4>` tag of `class="col blue"`.I seen online to use `preg_match_all()` I am not familiar with regular expression... any help please

Answer: 
Well, as often in life, you have two options here (I assume that scraped page's content is stored in `$content` variable):

**The Way of ~~([Cthulhu](http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html))~~ Regex:**

```
$matches = array();
preg_match_all('#<div class="col blue">.+?<h4>([^<]+)#is', $content, $matches);
var_dump($matches[1]);

```

**The Way of DOM Parsing:**

```
$dom = new DOMDocument();
$dom->loadHTML($content);
$xpath = new DOMXpath($dom);
$elements = $xpath->query('//div[@class="col blue"]/h4');
foreach ($elements as $el) {
   var_dump($el->textContent);
}

```

And the real question, of course, is what way to choose. 

The first option is short, concise, and quite seductive overall. I admit I would use it once, twice, or (`pony he comes`) even more - IF and only IF I know that HTML I work with will always come to me normalized AND I can cope with its structure suddenly changing in a non-predictable way.

The second option is slightly bigger, and may look too generic. Yet it's way more flexible and resilient to changes in the source, in my opinion. 

For example, consider what happens if some 'blue-colored' divs in the source HTML may come out without `<h4>` element. To work correctly in such conditions, the regex will have to become much more sophisticated. And the XPath query? Won't change - even a slightest bit.
Don't use Regular Expressions to parse HTML. It may seem difficult to use libraries and dedicated solutions. You can find a lot of "Don't use regex" answers out there.

I'd recommend [SimpleHTMLDOM](http://simplehtmldom.sourceforge.net/) which is [simple to use](http://simplehtmldom.sourceforge.net/manual.htm).

```
    <?php
// include necessary classes first.
// e.g. include('simple_html_dom.php'); // assuming the file is in same folder as the php file. Or include it at first or you will get a fatal error.
    $html = str_get_html('<div class="col blue">
            <img  src="/en/media/Dentalscreenresized.jpg" />
            <h4>This is line i want to scrape</h4>
            <p class="date">12 Sep
                <span class="year">2012</span></p>
            <p>13 people were diagnosed with oral cancer after last year&rsquo;s Mouth Cancer Awareness Day. Ring 021-4901169 to arrange for a free screening on the 19th September.</p>
            <p class="readmore"><a href="/en/news/abcd.html">Read More</a></p>
            <p class="rightreadmore"><a href="http://www.xyz.ie/en/news/">See all News&nbsp;&nbsp;&nbsp;</a></p>
        </div>
    ');

    $h4 = $html->find('h4.col.blue');
    ?>

```

Now $h4 contains all elements with the h4 tag with col and blue classes.