使用 preg_match 从此 html 中抓取经度和纬度


Scrape longitude and latitude from this html using preg_match

我正在尝试使用正则表达式从此 html 中提取经度和纬度坐标。这只是 http://www.bricodepot.fr/troyes/store/storeDetails.jsp?storeId=1753 完整 html 的一个片段,所以我需要能够搜索整个目标页面 html 并匹配此块的经度和纬度:

<div class="coordinates">
    <i></i>
    <strong>Latitude :</strong> 46.369384765625<strong>Longitude :</strong> 2.56929779052734</div>
</div>

这大约是我目前所能得到的:

preg_match("/<strong>Latitude :<'/strong> (.*?)/", $input_line, $output_array);

这给了:

数组( [0] => 纬度 : [1] =>)

知道我怎样才能得到绳索吗?

你快到了!

preg_match_all("<strong>L(at|ong)itude :<'/strong>'s(['w'.]*)?", $input_line, $output_array);

生成的数组将如下所示:

Array
(
    [0] => Array
        (
            [0] => <strong>Latitude :</strong> 46.369384765625
            [1] => <strong>Longitude :</strong> 2.56929779052734
        )
    [1] => Array
        (
            [0] => at
            [1] => ong
        )
    [2] => Array
        (
            [0] => 46.369384765625
            [1] => 2.56929779052734
        )
)
preg_match_all("/<strong>Latitude :<'/strong> (['d'.]+)<strong>Longitude :<'/strong> (['d'.]+)/", $input_line, $output_array);

您可以做的最简单的事情是先去除标签。那么你的正则表达式可能会简单得多,更易于维护。

通过剥离标签,您最终会得到:

Latitude : 46.369384765625Longitude : 2.56929779052734

除了这个正则表达式:

/(?:(Latitude|Longitude) : (['d'.]+))/

你最终会得到这样的东西:http://ideone.com/JGZOZi