Jump to content

Recommended Posts

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
https://forums.phpfreaks.com/topic/104670-longitude-latitude/#findComment-535823
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
https://forums.phpfreaks.com/topic/104670-longitude-latitude/#findComment-535933
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.