0

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&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

user1529342
  • 354
  • 1
  • 2
  • 11

4 Answers4

1

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 which is simple to use.

    <?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.

AKS
  • 4,618
  • 2
  • 29
  • 48
1

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) 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.

raina77ow
  • 103,633
  • 15
  • 192
  • 229
0

Don't use regular expressions to parse/scrape info from HTML, try a DOM parser like the one built into PHP.

newenglander
  • 2,019
  • 24
  • 55
0

Use DOM and Xpath. put your html data in $html.

$dom = new DOMDocument('1.0', 'UTF-8');
@$dom->loadHTML($html);
$xmlElements = simplexml_import_dom($dom);

$divs = $xmlElements->xpath("//div[@class='col blue']");
foreach($divs as $div)
{
  $heading = $div->h4;
  var_dump($heading);
}

Additional Note:

Don't use regular expressions to parse/scrape info from HTML. Its a Bad technique
user1518659
  • 2,198
  • 9
  • 29
  • 40