简体   繁体   English

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

[英]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

Demo 演示

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;

https://3v4l.org/qdIl4 https://3v4l.org/qdIl4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正则表达式验证纬度和经度的输入格式 - Regex Expression to validate input format of Latitude and Longitude Mapbox JS - 从 PHP MySql 获取经度和纬度 - Mapbox JS - Fetching Longitude and Latitude from PHP MySql 通过Javascript从String中提取纬度和经度 - Extract Latitude and Longitude by Javascript from String 从格式(47.35275,8.55709)中提取纬度和经度 - Extract latitude and longitude from the format (47.35275, 8.55709) 在JavaScript中,如何从字符串中提取纬度和经度 - In JavaScript, How to extract latitude and longitude from string 纬度/经度正则表达式 - Latitude/Longitude Regular Expression 从函数中获取经度和纬度值 - Grabbing longitude and Latitude value from a function 在ASP.NET MVC中使用地理编码,并希望将经纬度从JS传输到控制器中的某些函数 - Using geocoding in asp.net mvc and want to transfer latitude and longitude from JS to some function in controller jQuery,用于验证纬度和经度正则表达式 - jQuery for validating latitude and longitude regex 如何从函数获取纬度经度并将其发送到新的google.maps.LatLng(latitude,longitude)javascript - how to get latitude longitude from function and send it to new google.maps.LatLng( latitude ,longitude ) javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM