Remove Spaces from a String using PHPt.

This is quite a funny one for me. As you may or may not know I’m not a guru PHP developer but I do know a little from my time as a web designer/developer. Today I had to do something as simple as removing spaces from a variable string using PHP.

After a quick think about how I was going to do this I put together some code that would remove the spaces.

$mobile = ” 0400 000 000″;
//echo $mobile;
$mobile = trim($mobile);
$numcount = strlen($mobile);
//echo $numcount;
$ptr=0;

while ($i <= $numcount) { //Loop until testing of all numerals in ph no
if ($mobile{$ptr} != ” “) {
$ptr++;
}else{
$mobiletemp = substr($mobile, 0, $ptr);
echo “mobiletemp:”.$mobiletemp.”\n”;
$mobile = $mobiletemp.substr($mobile,($ptr+1),$numcount);
echo “mobile :”.$mobile;
$ptr++;
};
$i++;
}

So here I was really proud of myself for cooking up such an elaborate chunk of code that would do what I needed. During the process of writing the code I posted a question on a developer forum asking for a quick way of removing the spaces. By the time I finished my piece of code I had a response from the developer forum.

And here was their solution

$mobile = str_replace ( ‘ ‘, ”, $mobile );

That’s right. Just one line of code to do the same thing as my 18 lines of code. Same solution different method.
Hopefully someone who like me needs to know how to do this in a hurry and will find this post.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

19 Responses to “Remove Spaces from a String using PHPt.”

  1. Joshua Jonah Says:

    “Hopefully someone who like me needs to know how to do this in a hurry and will find this post. If you find this post and it saves you some time, please leave a comment.”

    Yeah, that would be me. Cheers:)

  2. Anthony Linton Says:

    Thanks, a great help!

  3. smily Says:

    It certainly helped me, thankyou very much!

  4. Brian Says:

    I found this one before anything else when looking for the subject, thanks for posting it!

  5. Stephan Says:

    Luckily i found this :D :D :D

  6. Julian Says:

    Glad i found this, great!!! :D

  7. Basil Says:

    great work done i was searching for this

  8. Ian Says:

    Thanks very good and keep up posting these little tips.

  9. Rob Cockerham Says:

    Thanks! I guess I can’t ask for a tighter solution than ONE LINE. :)

  10. John Tantoco Says:

    Fantastic work! Saved me a lot of time and will come in handy.

  11. KRASTA Says:

    Nice one ! Was about to dig around and do some serious revision, but saved me the hastle !

    Many thanks ! :)

  12. Janne Mikola Says:

    I just had a similar problem with the website I wrote there and Googled for help and found this. Thanks.

  13. jesse Says:

    a big help. thanks!

  14. m Says:

    Thanks! I was frantically trying to eliminate spaces in my filenames so the website could zip them!

  15. Castro Says:

    thanks man. great help

  16. Dmitry Says:

    Thanks man. Great help

  17. Matt Says:

    Great, I had pretty much written the same chunk of code as you, then one last google search and hey presto all that work reduced to one handy line. Thanks.

  18. dwek Says:

    Wow! Thanks for posting that function! It solved my file upload problem in an instant! :D

  19. Philip Says:

    hah that code saved my day!