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.



September 8th, 2006 at 11:23 am
“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:)
October 30th, 2006 at 7:53 pm
Thanks, a great help!
November 13th, 2006 at 11:45 am
It certainly helped me, thankyou very much!
January 25th, 2007 at 3:56 pm
I found this one before anything else when looking for the subject, thanks for posting it!
February 12th, 2007 at 1:43 am
Luckily i found this
:D 
February 14th, 2007 at 11:26 pm
Glad i found this, great!!!
March 15th, 2007 at 3:08 pm
great work done i was searching for this
March 17th, 2007 at 8:42 am
Thanks very good and keep up posting these little tips.
April 14th, 2007 at 4:03 am
Thanks! I guess I can’t ask for a tighter solution than ONE LINE.
April 25th, 2007 at 8:20 pm
Fantastic work! Saved me a lot of time and will come in handy.
August 13th, 2007 at 8:44 am
Nice one ! Was about to dig around and do some serious revision, but saved me the hastle !
Many thanks !
September 4th, 2007 at 1:58 am
I just had a similar problem with the website I wrote there and Googled for help and found this. Thanks.
February 27th, 2008 at 11:41 am
a big help. thanks!
April 1st, 2008 at 1:53 am
Thanks! I was frantically trying to eliminate spaces in my filenames so the website could zip them!
May 1st, 2008 at 11:11 pm
thanks man. great help
June 5th, 2008 at 3:15 am
Thanks man. Great help
June 10th, 2008 at 11:59 pm
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.
June 17th, 2008 at 10:33 pm
Wow! Thanks for posting that function! It solved my file upload problem in an instant!
June 25th, 2008 at 1:31 pm
hah that code saved my day!