Page 2 of 2

Posted: Sat Jul 08, 2006 5:20 pm
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...

Posted: Sat Jul 08, 2006 5:25 pm
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:

Posted: Sat Jul 08, 2006 5:30 pm
by shoebappa
I hate when people catch me doing that...

Posted: Sat Jul 08, 2006 5:34 pm
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);

Posted: Sat Jul 08, 2006 5:35 pm
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.

Posted: Sat Jul 08, 2006 5:36 pm
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:

Posted: Sat Jul 08, 2006 5:38 pm
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.

Posted: Sat Jul 08, 2006 5:55 pm
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*

Posted: Sat Jul 08, 2006 5:56 pm
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.

Posted: Sat Jul 08, 2006 5:57 pm
by Weirdan
Does the {2,} mean two or more
yes