Strip Whitespace From a String

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

User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Strip Whitespace From a String

Post 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.
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

Code: Select all

preg_replace("/\s*/","",$string);
?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

very nice if a little boring. next poster make it a little more esoteric.
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

There is no simpler way than ...

Code: Select all

str_replace(" ", "", $string);
[/syntaxt]
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

ahh but that doesn't remove \n \r or \t
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

is that faster or slower than the preg_replace one, do you know?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
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.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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... :wink:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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. :wink:
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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 :wink:
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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 :wink:
yeah also you're assigning a string inside the timing code which is then adding that to the measurement.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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. :D
Post Reply