crazyguy30 Posted May 8, 2008 Share Posted May 8, 2008 Hi, This is my first post. I'm quite new to regex and was wondering if any of you had any ideas for the regex for longitude / latitude? Example: Longitude - 36° 51' S Latitude - 174° 46' E Any help would be appreciated. Thanks Quote Link to comment Share on other sites More sharing options...
aCa Posted May 8, 2008 Share Posted May 8, 2008 Could you provide us with some more info? Is your example the text you will check and only want the numbers? If that is the case somthing like this might solve your problem: preg_match_all('/([0-9]+)°\s([0-9]+)'/', $string, $result); Quote Link to comment Share on other sites More sharing options...
crazyguy30 Posted May 8, 2008 Author Share Posted May 8, 2008 Hi, Thanks for the reply. Here is some background information: Latitude: * Degrees can be any integer number from 0 to 90 * Minutes can be any integer number from 0 to 59 * Seconds can be any integer number from 0 to 59 * Direction can be either E or W An example 35°48'35 W Longitude: * Degrees can be any integer number from 0 to 180 * Minutes can be any integer number from 0 to 59 * Seconds can be any integer number from 0 to 59 * Direction can be either N or S An example 175°15'19 S It would be helpful if the regex could ensure the separators such as ° and ' were in place. The text i plan to evaluate this against is coming from an input form. Thanks Quote Link to comment Share on other sites More sharing options...
effigy Posted May 8, 2008 Share Posted May 8, 2008 <pre> <?php $lat_pattern = '/ \A (90|[1-8]\d|\d) \xBA (5\d|[1-4]\d|\d) \' (5\d|[1-4]\d|\d) \s* [EW] \z /x'; $long_pattern = '/ \A (180|1[0-7]\d|\d\d?) \xBA (5\d|[1-4]\d|\d) \' (5\d|[1-4]\d|\d) \s* [NS] \z /x'; ### Generate test data. $degree = pack('C*', 0xBA); $dirs = array('N', 'S', 'E', 'W'); for ($i = 0; $i < 1000; $i++) { $deg = rand(0, 200); $min = rand(0, 100); $sec = rand(0, 100); $dir = $dirs[rand(0, 3)]; $tests[] = "$deg$degree$min'$sec$dir"; } echo '<table border="1">'; echo '<tr><th>Value</th><th>Latitude</th><th>Longitude</th></tr>'; foreach ($tests as $test) { echo "<tr><td>$test</td>", '<td>', (preg_match($lat_pattern, $test) ? 'Y' : 'N'), '</td><td>', (preg_match($long_pattern, $test) ? 'Y' : 'N'), '</td></tr>'; } echo '</table>'; ?> </pre> Quote Link to comment Share on other sites More sharing options...
crazyguy30 Posted May 8, 2008 Author Share Posted May 8, 2008 Thanks so much that works perfect. I can't wait to learn the finer details of regex. Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.