1
votes

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:

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.

4
/setMap\((\d+\.\d*), (\d+\.\d*)/ - raina77ow

4 Answers

1
votes

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
1
votes

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

0
votes

You can try

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

Optimized and regex doesn't really go hand in hand with this simple parsing.
Here is a "optimized" solution using Substr and 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