0

I'm trying to use a regex to extract just the longitude and latitude coordinates from this html. This is just a snippet of the full html from http://www.bricodepot.fr/troyes/store/storeDetails.jsp?storeId=1753 so I need to be able to search through the whole targets page html and match the longitude and latitude from this block:

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

This is about as close as I can get at the moment:

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

which gives:

Array ( [0] => Latitude : [1] => )

Any idea how I can get the cordinates?

Ben Paton
  • 1,432
  • 9
  • 35
  • 59

3 Answers3

0

You're almost there!

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

And the resulting array will be something like:

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
        )
)
brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • Thank you, some good answers but this is the solution I ended up using. I like your words of encouragement of almost being there. I wish I understood regex better. – Ben Paton Nov 05 '13 at 19:45
  • Good luck @BenPaton! You might check out regex101.com too for learning more about regex ;) – brandonscript Nov 05 '13 at 20:31
  • Had to add a minus infront of the second longitude part as sometimes the longitude is minus. Use regex101.com to test this addition. Good site! Thanks. – Ben Paton Nov 05 '13 at 22:42
0
preg_match_all("/<strong>Latitude :<\/strong> ([\d\.]+)<strong>Longitude :<\/strong> ([\d\.]+)/", $input_line, $output_array);
andypp
  • 249
  • 2
  • 12
0

The easiest thing you can do is to strip the tags out first. Then your RegExp could be a lot simpler and maintainable.

By stripping the tags, you end up with:

Latitude : 46.369384765625Longitude : 2.56929779052734

Along with this RegExp:

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

You end up with something like this: http://ideone.com/JGZOZi

Ayman Safadi
  • 11,502
  • 1
  • 27
  • 41