Possible Duplicate:
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’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 </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
Well, as often in life, you have two options here (I assume that scraped page’s content is stored in
$contentvariable):The Way of
(Cthulhu)Regex:The Way of DOM Parsing:
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.