I'm trying to pull the weather from the weather network. This code gives it to me, but it looks pretty long, and returns two values in the array (that's why I need to return $output[1] and not $output[0]), when I only want it to return one. Any ideas?
$url=file_get_contents("http://www.theweathernetwork.com/weather/cans0057?ref=homecity");
preg_match('开发者_运维知识库/<div id="obs_conds" class="hslice">.*?<img.*?alt="(.*?)".*?<\/div>/s',$url,$output);
print_r($output[1]);
I think it's great. Maybe you can make shortcut from it
preg_match('/class="hslice">.*?<img.*?alt="(.*?)"/s',$url,$output);
I believe preg_matches returns the whole match in the the first element of the array $output[1] and $output[1] the text that matched the first captured parenthesized subpattern, and so on (http://php.net/manual/en/function.preg-match.php). Regarding the regex optimization, I guess you're trying to pull everything inside <div id="obs_conds" class="hslice">
, if that is the case I would try to do a dom search instead.
On the other hand maybe the best solution is to contact the site and set a webservice client. It seems to me that they do have the infrastucture for that.
精彩评论