[英]PHP regex expression extract latitude & longitude from JS function
I am using the simple_html_dom PHP library to scrape some content of a page. 我正在使用simple_html_dom PHP库来抓取页面的某些内容。 I would like to extract the latitude and longitude from the page but I need a regex expression to access these value since these values are only available on the page in a Javascript function:
我想从页面中提取纬度和经度,但是我需要一个正则表达式来访问这些值,因为这些值仅在Javascript函数中的页面上可用:
function loadMap() { setTimeout("setMap(39.364016, 3.226783, 'Hotel Casa',
'icon.png', 'key')", 200)};
I got the above example in a string. 我得到了上面的示例字符串。 What would be a well optimized regex expression (using PHP) to extract the latitude (39.364016) and the longitude (3.226783) from this string?
一个很好优化的正则表达式(使用PHP)从该字符串中提取纬度(39.364016)和经度(3.226783)是什么? I am new to regex expressions so my attempts so far have not been successful, I hope someone could can help me out.
我是regex表达式的新手,所以到目前为止,我的尝试还没有成功,我希望有人可以帮助我。 Thank you.
谢谢。
Use this regex: 使用此正则表达式:
/setMap\((\-?\d+\.?\d*), ?(\-?\d+\.?\d*)/
Details 细节
setMap\( match that string, literally, with the open parentheses
\-? optional minus symbol
\d+ a digit, one or more times
\.? a literal dot, optional (in the rare case you get an integer)
\d a digit, 0 or more times (in the rare case you get an integer)
, ? an comma followed optionally by a space
Using named captures, which you might find a bit clearer: 使用命名捕获,您可能会发现它更清晰一些:
<?php
$html = <<<HTML
<html>
...
function loadMap() { setTimeout("setMap(39.364016, 3.226783, 'Hotel Casa',
'icon.png', 'key')", 200)};
...
</html>
HTML;
$regex = '/setMap\((?P<latitude>[0-9\.\-]+), (?P<longitude>[0-9\.\-]+)/';
$matches = [];
preg_match($regex, $html, $matches);
echo "Latitude: ", $matches['latitude'], ", Longitude: ", $matches['longitude'];
// Latitude: 39.364016, Longitude: 3.226783
你可以试试
/[0-9]{1,3}[.][0-9]{4,}/
Optimized and regex doesn't really go hand in hand with this simple parsing. 优化和正则表达式实际上与这种简单的分析并没有紧密结合。
Here is a "optimized" solution using Substr and strpos. 这是使用Substr和strpos的“优化”解决方案。
$str = <<<EOD
function loadMap() { setTimeout("setMap(39.364016, 3.226783, 'Hotel Casa',
'icon.png', 'key')", 200)}
EOD;
$pos = strpos($str, "setMap(") + 7; //find position of setMap(
$latlon = Substr($str, $pos, strpos($str, ", '")-$pos); // substring from setMap to `, '`
List($lat, $lon) = explode(", ", $latlon); // explode the latlon to each variable.
Echo $lat . " " . $lon;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.