Page 1 of 2
Strip Whitespace From a String
Posted: Sat Jul 08, 2006 4:04 pm
by Ollie Saunders
From inside as well as on the left and right.
I know there are about a million ways of doing this. Which is the best though?
Do post. Do post.
Posted: Sat Jul 08, 2006 4:08 pm
by shoebappa
Posted: Sat Jul 08, 2006 4:19 pm
by Ollie Saunders
very nice if a little boring. next poster make it a little more esoteric.
Posted: Sat Jul 08, 2006 4:24 pm
by jamiel
There is no simpler way than ...
Code: Select all
str_replace(" ", "", $string);
[/syntaxt]
Posted: Sat Jul 08, 2006 4:31 pm
by Ollie Saunders
ahh but that doesn't remove \n \r or \t
Posted: Sat Jul 08, 2006 4:32 pm
by s.dot
Code: Select all
$text = str_replace(array(' ',"\r","\n","\r\n","\t"),'',$text);
Posted: Sat Jul 08, 2006 4:42 pm
by Ollie Saunders
is that faster or slower than the preg_replace one, do you know?
Posted: Sat Jul 08, 2006 4:50 pm
by s.dot
heh i was bored.
using a simple test script
Code: Select all
<?php
for($i=0;$i<50;$i++){
$string = 'hey im a string with some spaces
and a new line multiple spaces
with a few tabs
thrown in here
and a bunch of gargly goo that doesn\'t make any sense =] ';
$start = microtime(TRUE);
preg_replace("/\s*/","",$string);
$end = microtime(TRUE);
echo $end-$start.'<br />';
}
echo '<br /><br /><br />';
for($i=0;$i<50;$i++){
$string = 'hey im a string with some spaces
and a new line multiple spaces
with a few tabs
thrown in here
and a bunch of gargly goo that doesn\'t make any sense =] ';
$start = microtime(TRUE);
str_replace(array(' ',"\r","\n","\r\n","\t"),'',$string);
$end = microtime(TRUE);
echo $end-$start.'<br />';
}
?>
here are the results
preg_replace in the first batch
str_replace in the second batch
Code: Select all
0.00056004524230957
0.00051999092102051
0.00038719177246094
0.00032711029052734
0.00033283233642578
0.00032591819763184
0.00032210350036621
0.00032806396484375
0.00032281875610352
0.00032401084899902
0.00032687187194824
0.00032901763916016
0.00032711029052734
0.00032401084899902
0.00032901763916016
0.0003209114074707
0.00033020973205566
0.00032615661621094
0.00032520294189453
0.00033402442932129
0.00032496452331543
0.00032401084899902
0.00032210350036621
0.00032496452331543
0.00032711029052734
0.00032401084899902
0.00032496452331543
0.00040698051452637
0.00055217742919922
0.00058794021606445
0.00052595138549805
0.00033116340637207
0.00032305717468262
0.00032496452331543
0.00032520294189453
0.00032711029052734
0.00032496452331543
0.00032591819763184
0.00032186508178711
0.00036787986755371
0.00032186508178711
0.00032591819763184
0.00033092498779297
0.00032782554626465
0.00032711029052734
0.00032591819763184
0.00032496452331543
0.00032401084899902
0.00032901763916016
0.00032711029052734
2.8848648071289E-005
1.9073486328125E-005
1.8119812011719E-005
1.8119812011719E-005
1.8119812011719E-005
1.8119812011719E-005
1.8119812011719E-005
1.8119812011719E-005
1.9073486328125E-005
1.7881393432617E-005
1.7881393432617E-005
2.6941299438477E-005
1.8835067749023E-005
1.7881393432617E-005
1.7881393432617E-005
1.7881393432617E-005
1.7881393432617E-005
1.7881393432617E-005
1.7881393432617E-005
1.7881393432617E-005
1.7881393432617E-005
1.7881393432617E-005
1.7881393432617E-005
1.7881393432617E-005
1.7881393432617E-005
1.7881393432617E-005
1.7881393432617E-005
1.7881393432617E-005
1.7881393432617E-005
1.7881393432617E-005
1.8835067749023E-005
1.7881393432617E-005
1.7881393432617E-005
1.7881393432617E-005
1.9073486328125E-005
1.8119812011719E-005
1.8119812011719E-005
1.7881393432617E-005
1.7881393432617E-005
1.7881393432617E-005
1.8119812011719E-005
1.8119812011719E-005
2.3126602172852E-005
1.9073486328125E-005
1.8119812011719E-005
1.8119812011719E-005
1.8119812011719E-005
1.8119812011719E-005
1.8119812011719E-005
1.8119812011719E-005
It appears str_replace is marginably (although unnoticeably during runtime) faster
Posted: Sat Jul 08, 2006 4:52 pm
by Oren
ole wrote:is that faster or slower than the preg_replace one, do you know?
You can check this yourself easily. If you don't know how, just ask...

Posted: Sat Jul 08, 2006 4:53 pm
by RobertGonzalez
I would guess str_replace() would be faster than preg_replace because you are telling the function the exact string to locate and replace, where as in preg_replace, the function needs to match first against a pattern, the replace. I know strpos and strstr are faster than preg_match, so I am assuming that str_replace is faster that it's preg_* equivalent.
On a side note, your original post asked about removing whitespaces from the beginning, middle and end of a string. The '\n' and '\r' characters are not whitespace. They are actual characters that represent whitespace, so the original preg_replace and str_replace examples would work for your original post. Just my $0.02.

Posted: Sat Jul 08, 2006 5:07 pm
by Oren
scottayy, it'd be much better to run each test more than 50 times (1000 times sounds fine for me). Instead of printing each result, append it to a variable and then make an average

Posted: Sat Jul 08, 2006 5:08 pm
by Ollie Saunders
The '\n' and '\r' characters are not whitespace
that's a new idea to me. I always thought that was the whole point of the \s in regex because that matches all those characters too.
Posted: Sat Jul 08, 2006 5:09 pm
by Ollie Saunders
Oren wrote:scottayy, it'd be much better to run each test more than 50 times (1000 times sounds fine for me). Instead of printing each result, append it to a variable and then make an average

yeah also you're assigning a string inside the timing code which is then adding that to the measurement.
Posted: Sat Jul 08, 2006 5:14 pm
by s.dot
well
1) I did 50, for posting purposes, do you really want to see 1000 results posted in here?

and
2) the assigning of the string is not being calculated in the time, only the functions mentioned
and
3) i was just bored and said it was a 'simple' test
.. and the results are pretty conclusive, so the same result would be found in a more 'advanced' test
Posted: Sat Jul 08, 2006 5:17 pm
by Ollie Saunders
i was just bored and said it was a 'simple' test
Yeah I'll give you a break it was pretty good and much more than I expected. Thanks.
