Page 1 of 1

Removing a space from between digits

Posted: Thu Nov 14, 2002 8:47 am
by mikebr
This I thought would be simple but I can't seem to find a way of removing a space from between two numbers, it is actually a phone number with any prefix needed before the number which will be seperated by a space, take $phone = "456 56789"; I just need to know if the variable contains digits and or digits and spaces.

I thought that I could replace the " " with "" using the strtr function then use is_numeric but the strtr function does not seem to work with spaces, I thought there might be a function like trim that would remove all the spaces but there does not seem to be one in the manual, isn't there any simple way of doing this?

Posted: Thu Nov 14, 2002 9:02 am
by twigletmac
Use str_replace():

Code: Select all

/* Remove all spaces from a string */
$string_without_spaces = str_replace(' ', '', $string_with_spaces);
Mac

Posted: Thu Nov 14, 2002 9:03 am
by kcomer
try using the str_replace function.

Keith

Posted: Thu Nov 14, 2002 10:24 am
by mikebr
Yes, I guess thats simple and it works. I guess I just though str_replace wouldn't work with spaces when strtr didn't.

Thanks