Question

I have a coordinate in the format

43-09-42.0000N

I want to split it into its component parts

43 09 42.000 N

I'm using.

$parts = preg_split('/[-]/', $LON);

and it gives me

Array ( [0] => 43 [1] => 09 [2] => 42.0000N )

But I can't figure out who to get the Letter on the end to spilt out.

Any help appreciated I am a complete dope when it comes to regex.

Was it helpful?

Solution

If you want to split it (which could be useful if the format changes) you could use something like:

$parts = preg_split('/-|(?<=\d)(?=[a-z])|(?<=[a-z])(?=\d)/i', $LON);

If the format doesn't change you are better off using something like Mark's answer.

If the format changes slightly you can use a regex match which allows for some variations.

OTHER TIPS

$string = '43-09-42.0000N';

$result = sscanf($string, '%d-%d-%[0-9.]%s');
var_dump($result);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top