Jump to content

Longitude - Latitude


crazyguy30

Recommended Posts

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

Link to comment
Share on other sites

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);

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.