Question

I have simple problem.

I need to open for example this page http://www.50states.com/ in php.

Then I need to open every state and from page of state for (example: Alabama), I need to parse name of state, capital, location. I want to do it with simple html dom library http://simplehtmldom.sourceforge.net/

And I want to do it on every state. How it is possible???

//parser.php
include simple_html_dom.php;
$document = file_get_html($site);

foreach($document->find('a') as $e) {
    echo $e->href . '<br>';
}

So what now? Please help. I think now I need only hrefs with states and then I must open it... So?

Was it helpful?

Solution

Why not feed it with the state name on the url? Consider this as an example:

<?php

include 'simple_html_dom.php';

$state = 'alabama';
$main_url = 'http://www.50states.com/' . $state . '.htm';
$html = file_get_html($main_url);

$state_info = null;
$capital_city = '';
foreach($html->find('ul[class=bulletedList]') as $key => $value) {
    $state_info = $value;
    // Get particular value (traverse DOM)
    // Sample: search for capital city
    if(strpos($value->children(0)->children(0)->innertext, 'Capital City:')) {
        $capital_city = $value->children(0)->children(0)->innertext;    
    }
}

echo $state_info;

echo $capital_city;

?>

For more in-depth information, you should check out the manual, its pretty well documented anyways.

http://simplehtmldom.sourceforge.net/manual.htm

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top