0

你能看到我的正则表达式与一些抓取的 html 中的经度和纬度相匹配的地方出错了吗?

该脚本应该通过使用 file_get_contents 加载一些 html 然后使用正则表达式和 preg_match 来提取纬度和经度来工作。目前,下面的脚本正在输出纬度和经度的空白,我不确定出了什么问题,正则表达式对我来说不是一个非常强大的领域。谢谢。

$url = 'http://www.homebase.co.uk/webapp/wcs/stores/servlet/StoreLocatorFlow?slsid=658';

$scrapedPage = file_get_contents($url);

返回的 html 中有一行如下:

<p class="geo"> <abbr class="latitude" title="52.19166">52.19166</abbr> <abbr class="longitude" title="-2.23108">-2.23108</abbr> </p> 

然后我们做 preg_match:

preg_match('/class="latitude"\s*title="([^"]+)"/', $scrapedPage, $lat);
preg_match('/class="longitude"\s*title="([^"]+)"/', $scrapedPage, $lon);

echo '<latitude>'.$lat[1].'</latitude>';
echo '<longitude>'.$lon[1].'</longitude>';
4

0 回答 0