I'm trying to use PHP to return three simple variables from a web page. The web page is: http://areacode.org/519 My code is below:
<!doctype html>
<html>
<head>
<title>Untitled Document</title>
</head>
<?php
//variables
$quickurl = idk;
$country = idk;
$State = idk;
$city = idk;
//end of variables
$test = file_get_contents("http://areacode.org/519");
//$strip = strip_tags($test); Not sure if this is needed.
echo ("<center>Info about Area code! Country: ".$country.". State: ".$state.". City: ".$city.". Quick reference URL: ".$quickurl."</center>");
?>
<body>
</body>
</html>
I am attempting to strip out the country, state, and major city and the quick url. I have looked around, but no help. I think I may just be a little confused. Any help?
All you need is DOM and XPath:
$dom = new DOMDocument;
@$dom->loadHTMLFile('http://areacode.org/519');
$xpath = new DOMXPath($dom);
$data = $xpath->query("//div[@class='info']//span[@class='value']");
list($country, $state, $city, $url) = array_map(function ($node) {
return trim($node->nodeValue, "\xc2\xa0"); // strip non-breaking spaces found on source
}, iterator_to_array($data));
var_dump($country, $state, $city, $url);
This gave me the following:
string(6) "Canada"
string(7) "Ontario"
string(6) "London"
string(16) "areacode.org/519"
That's all!
Use $pos = strpos($test, "label"); for get the position of the data you are looking for, and then substr(); to catch it, easy to start. Or u can use the correct way: http://us1.php.net/manual/es/domdocument.getelementsbytagname.php