Jump to content
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   

Link to comment
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

 

Link to comment
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. 

Link to comment
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 🤪.

Link to comment
Share on other sites

 Share

×
×
  • Create New...