Strip Whitespace From a String
Moderator: General Moderators
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Strip Whitespace From a String
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.
I know there are about a million ways of doing this. Which is the best though?
Do post. Do post.
Code: Select all
preg_replace("/\s*/","",$string);- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
-
jamiel
- Forum Contributor
- Posts: 276
- Joined: Wed Feb 22, 2006 5:17 am
- Location: London, United Kingdom
There is no simpler way than ...
Code: Select all
str_replace(" ", "", $string);
[/syntaxt]- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Code: Select all
$text = str_replace(array(' ',"\r","\n","\r\n","\t"),'',$text);Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
heh i was bored.
using a simple test script
here are the results
preg_replace in the first batch
str_replace in the second batch
It appears str_replace is marginably (although unnoticeably during runtime) faster
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 />';
}
?>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-005Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.
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.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
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
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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK