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

Post by shoebappa »

scottayy, I think the sum of the 1000 runs is what was meant.

ole, Yeah, I mean just cause it's a character representing whitespace in regex and XML and HTML a tab and newline are definately considered whitespace. A space is a character to... not sure where Everah was coming from. Just cause php and regex interpret ' ' as a space character, it's still a character just like \n and \t and \r...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

OK. I was wrong. Think the term is "talking out of his rear". No more peanuts for me, it is making me silly... :oops:
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

I hate when people catch me doing that...
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

lol.

Anyway I just realised I didn't want to remove all whitespace only where there is more than one contigous character of it. So I'm using this and I'm quite sure that its the only sensible way of doing it:

Code: Select all

preg_replace('/\s{2,}/',' ',$value);
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Well, here's a little bit of a better test, ran 1,000,000 times, added to an array, then averaged. Each ran separately.

Code: Select all

$times = array();

for($i=0;$i<1000000;$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);
	//DIFFERENT METHOD HERE
	$end = microtime(TRUE);
	
	$times[] = $end-$start;
	
}

echo array_sum($times)/1000000;
Results:

Code: Select all

preg_replace
0.00035032083010674

str_replace
0.00002033275658295
And during 1,000,000 iterations, the str_replace took MUCH noticably less time. So, str_replace is faster :P
This was kinda fun.
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
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

ole wrote:lol.

Anyway I just realised I didn't want to remove all whitespace only where there is more than one contigous character of it. So I'm using this and I'm quite sure that its the only sensible way of doing it:

Code: Select all

preg_replace('/\s{2,}/',' ',$value);
fook! :lol:
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
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

shoebappa wrote:I hate when people catch me doing that...
Yeah, I just associated the term white space with a 'white space' not a return, new line, tab or any other non-printing character. My bad.
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

ole wrote:

Code: Select all

preg_replace('/\s{2,}/',' ',$value);
Does the {2,} mean two or more and not just 2. I'm curious if it means the same as say \s\s*
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

shoebappa wrote:Does the {2,} mean two or more and not just 2. I'm curious if it means the same as say \s\s*
It means two or more.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Does the {2,} mean two or more
yes
Post Reply