jonmrich jonmrich -4 years ago 65
PHP Question

Proper syntax for scraping plugin

I'm using this excellent plugin to scrape some content from a specific website: http://simplehtmldom.sourceforge.net/manual.htm.

An example of what I'm trying to scrape through.

<section class="details">

<div class="address">
<div class="zip-code-country">
<span>USA</span>
</div>
</div>
<div class="phone">
<a href="tel:+1234567890">(123)456-7890</a>
</div>
<ul>
<li><a href="mailto:test@example.com">Email</a></li>
</ul>
</section>


The relevant part of my code is like this:

foreach($html->find('section.details') as $part2) {

$item2['zip'] = $part2->find('div.zip-code-country span', 0)->plaintext; //works perfectly
$item2['phone'] = $part2->find('div.phone a', 0)->plaintext; //works perfectly
$item2['email'] = $part2->find('ul li a[href]', 0)->plaintext; //issue is here
$pieces2[] = $item2;
}


When I try to get
email
, it returns the anchor text (i.e., "Email). What I want is for it to return "test@example.com".

Thoughts on how to get this syntax correct?

Answer Source

You would want to do:

$email= $part2->find('ul li a[href]', 0)->href;
$item2['email'] =str_replace('mailto:', '', $email);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download