Removing a space from between digits

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Removing a space from between digits

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Use str_replace():

Code: Select all

/* Remove all spaces from a string */
$string_without_spaces = str_replace(' ', '', $string_with_spaces);
Mac
kcomer
Forum Contributor
Posts: 108
Joined: Tue Aug 27, 2002 8:50 am

Post by kcomer »

try using the str_replace function.

Keith
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Post 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
Post Reply