Larry Ullman's Book Forums

# Separating numbers and alphanumerics

## Recommended Posts

Hi Larry,

I hope all is well with you.

I have what I thought was a simple problem with a simple solution.Â Â ðŸ˜„ðŸ¤£

I have in a mysql database a table with data so:Â  12345/98765 (they are co-ordinates).

I need to subtract a number from the left number and a different number from the right number, and then put them back.

Thus I need to assign the left hand number to a variable, and the right hand number to another variable.Â  Then I can do the subtraction and replace the co-ordinates.

The two numbers aren't necessarily always the same number of digits.

I have thought about str_replace, ltrim, rtrim, strpos etc. but none of them cut the mustard.Â  I'd rather do it in php that use LEFT() and RIGHT() in MySql.Â Â

Any ideas?Â  I have now wasted a whole afternoon on this.Â ðŸ™„

MaxÂ Â Â

##### Share on other sites

Sorry, this should be under PHP 6 and MySQL 5.

##### Share on other sites

Finally got it:

Â

\$map1 = 12345,98765Â  //Imagined co-ordinate

\$map2 = substr_replace(\$map1, '', strrpos(\$map1, ',', 0));Â  //Â  Gives the left hand number
Â Â  Â
\$map3 = substr_replace(\$map1, '', 0 , strrpos(\$map1, ',', 0) + 1);Â  //Gives the right hand number

RESULT:

Map1 = 12345

Map2 = 98765

Regards

Â

Max

Â

##### Share on other sites

Hey Max. Kudos for figuring it out and thanks for sharing the solution. The first thought that came to my mind would be to store the coordinates in separate columns. In other words, split the coordinates upon receipt and then store them in two columns. That'll make future work easier to do.Â

##### Share on other sites

Hi Larry,

That's what I would normally do, but I wasÂ copying and pasting a lot of data from my Garmin GPS text dump and didn't think about it at the time.Â  Forewarned is forearmed...or whatever!

Sadly, this is what passes for fun for meÂ ðŸ¤ª.