PHP正则表达式从JS函数中提取纬度和经度

时间:2022-11-19 20:56:25

I am using the simple_html_dom PHP library to scrape some content of a page. 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:

我正在使用simple_html_dom PHP库来抓取页面的某些内容。我想从页面中提取纬度和经度,但我需要一个正则表达式来访问这些值,因为这些值只能在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? I am new to regex expressions so my attempts so far have not been successful, I hope someone could can help me out. Thank you.

我在字符串中得到了上面的例子。什么是一个优化良好的正则表达式(使用PHP)从该字符串中提取纬度(39.364016)和经度(3.226783)?我是正则表达式的新手,所以到目前为止我的尝试还没有成功,我希望有人可以帮助我。谢谢。

4 个解决方案

#1


1  

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

#2


1  

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

Demo

#3


0  

You can try

你可以试试

 /[0-9]{1,3}[.][0-9]{4,}/ 

#4


0  

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;

https://3v4l.org/qdIl4

#1


1  

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

#2


1  

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

Demo

#3


0  

You can try

你可以试试

 /[0-9]{1,3}[.][0-9]{4,}/ 

#4


0  

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;

https://3v4l.org/qdIl4