Strip Whitespace From a String
Moderator: General Moderators
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...
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...
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
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:
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);Well, here's a little bit of a better test, ran 1,000,000 times, added to an array, then averaged. Each ran separately.
Results:
And during 1,000,000 iterations, the str_replace took MUCH noticably less time. So, str_replace is faster 
This was kinda fun.
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;Code: Select all
preg_replace
0.00035032083010674
str_replace
0.00002033275658295This 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.
fook!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);
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.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Does the {2,} mean two or more and not just 2. I'm curious if it means the same as say \s\s*ole wrote:Code: Select all
preg_replace('/\s{2,}/',' ',$value);