| | |
how to remove space from string?
![]() |
•
•
•
•
GAH! Just realized how old this thread is, how did it get resurrected?!
Someone read the Book of the dead I suppose ! Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Jul 2008
Posts: 151
Reputation:
Solved Threads: 25
If it solves your problem how you need it to solve your problem then its a solution. There is always more than one way to skin a cat.
If you're question/problem is solved don't forget to mark the thread as Solved!
-- Code I post is usually but not always tested. If it is tested it will be against 5.2.12 or 5.3.1
-- Code I post is usually but not always tested. If it is tested it will be against 5.2.12 or 5.3.1
Just to clear things up:
str_replace() will remove *all* occurrence of whitespace.
So it solves the problem in the original post.
The post:
Offers a solution that removes excess whitespace.
So if you want to turn:
into:
You could use the StripExtraSpace() function provided above.
Regex is really slow, I mean really really slow. So even a loop is likely to be faster.
I benchmarked the regex function and StripExtraSpace(). The regex function is twice as fast. However, an optimized version of StripExtraSpace() is faster then the regular expression.
Note the if/else is removed as this is implied in the while() loop. Also using the string's built in array index is a lot faster then substr().
The computational/time complexity of a regular expression is quite high. So an O(n), where n is the length of the string, such as the above can be just as fast, or faster.
Using a native PHP string function can speed things up:
This is about 30x faster the above two. This has a worst case O(log 2 n), but realistically it would be just 2-3 operations of str_replace().
Here is the benchmarks:
•
•
•
•
Is there php function to remove the space inside the string? for example:
$abcd="this is a test"
I want to get the string:
$abcd="thisisatest"
How to do that? thanks.
PHP Syntax (Toggle Plain Text)
$str = str_replace(' ', '', $str);
So it solves the problem in the original post.
The post:
•
•
•
•
Try this:
NOTE: this actually leaves one space between words. If you move the
$newstr = $newstr . substr($s, $i, 1);
after the while loop it will strip all spaces as you wanted.
function StripExtraSpace($s)
{
for($i = 0; $i < strlen($s); $i++)
{
$newstr = $newstr . substr($s, $i, 1);
if(substr($s, $i, 1) == ' ')
while(substr($s, $i + 1, 1) == ' ')
$i++;
}
return $newstr;
}
So if you want to turn:
PHP Syntax (Toggle Plain Text)
$str = "this is a string with excess whitespace";
into:
PHP Syntax (Toggle Plain Text)
"this is a string with excess whitespace";
You could use the StripExtraSpace() function provided above.
•
•
•
•
For starters, functions in loops should be avoided whenever possible. e.g.for($i = 0; $i < strlen($s); $i++)also, you're doing a lot of extra work here.
I would suggest something like this:
php Syntax (Toggle Plain Text)
<?php $sTestString = 'This is a stringwith lots of odd spaces and tabs and some newlines too lets see if this works.'; $sPattern = '/\s*/m'; $sReplace = ''; echo $sTestString . '<br />'; echo preg_replace( $sPattern, $sReplace, $sTestString );
Regular Expression removes ALL whitespace ( spaces, tabs, newlines) 0 or more times and also traverses multiple lines.
Put that in a file and run it in your browser, view the source and you'll see exactly what it has done.
No need to reinvent the wheel.
I benchmarked the regex function and StripExtraSpace(). The regex function is twice as fast. However, an optimized version of StripExtraSpace() is faster then the regular expression.
PHP Syntax (Toggle Plain Text)
function loopStripExtraWhitespaceOptimized($str) { $len = strlen($str); for($i = 0; $i < $len; $i++) { $newstr .= $str[$i]; while($str[$i] == ' ') { $i++; } } return $newstr; }
Note the if/else is removed as this is implied in the while() loop. Also using the string's built in array index is a lot faster then substr().
The computational/time complexity of a regular expression is quite high. So an O(n), where n is the length of the string, such as the above can be just as fast, or faster.
Using a native PHP string function can speed things up:
PHP Syntax (Toggle Plain Text)
function stripExtraWhitespace($str) { while($str != ($_str = str_replace(' ', ' ', $str))) { $str = $_str; } return $str; }
This is about 30x faster the above two. This has a worst case O(log 2 n), but realistically it would be just 2-3 operations of str_replace().
Here is the benchmarks:
PHP Syntax (Toggle Plain Text)
<?php function strStripWhitespace($str) { return str_replace(' ', '', $str); } function regexStripExtraWhitespace($str) { return preg_replace('/\s*/m', ' ', $str); } function loopStripExtraWhitespace($str) { $len = strlen($str); for($i = 0; $i < $len; $i++) { $newstr .= substr($str, $i, 1); while(substr($str, $i + 1, 1) == ' ') { $i++; } } return $newstr; } function loopStripExtraWhitespaceOptimized($str) { $len = strlen($str); for($i = 0; $i < $len; $i++) { $newstr .= $str[$i]; while($str[$i] == ' ') { $i++; } } return $newstr; } function loopStripExtraWhitespace2($str) { while($str != ($_str = str_replace(' ', ' ', $str))) { $str = $_str; } return $str; } function benchMark($function, $arg = null, $loops = 1000) { $start = microtime(1); for($i = 0; $i < $loops; $i++) { $function($arg); } return microtime(1)-$start; } ini_set('max_execution_time', 0); $str = "\t hello this is a string.\r\n"; for($i = 0; $i < 10; $i++) { $str .= $str; } echo benchMark('strStripWhitespace', $str, 1000); echo '<br />'; echo benchMark('regexStripExtraWhitespace', $str, 1000); echo '<br />'; echo benchMark('loopStripExtraWhitespace', $str, 1000); echo '<br />'; echo benchMark('loopStripExtraWhitespaceOptimized', $str, 1000); echo '<br />'; echo benchMark('loopStripExtraWhitespace2', $str, 1000); echo '<br />'; ?>
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
•
•
Join Date: Jul 2008
Posts: 151
Reputation:
Solved Threads: 25
Well you're definitely right. regex is definitely slower when compared to str_replace especially as the length of the string increases. Although
However adding those to an array of replacements in str_replace still drastically spanked the regular expression replacement.
I feel kinda stupid for overlooking to most obvious solution to a few year old thread haha.
return str_replace(' ', '', $str); is not the same as return preg_replace('/\s*/m', ' ', $str); as the regex covers ALL whitespace, newlines tabs etc etc. When i ran your benchmarks on 5.3b1 str_replace did not replace tabs and new lines in the output.However adding those to an array of replacements in str_replace still drastically spanked the regular expression replacement.
I feel kinda stupid for overlooking to most obvious solution to a few year old thread haha.
If you're question/problem is solved don't forget to mark the thread as Solved!
-- Code I post is usually but not always tested. If it is tested it will be against 5.2.12 or 5.3.1
-- Code I post is usually but not always tested. If it is tested it will be against 5.2.12 or 5.3.1
•
•
•
•
Well you're definitely right. regex is definitely slower when compared to str_replace especially as the length of the string increases. Althoughreturn str_replace(' ', '', $str);is not the same asreturn preg_replace('/\s*/m', ' ', $str);as the regex covers ALL whitespace, newlines tabs etc etc. When i ran your benchmarks on 5.3b1 str_replace did not replace tabs and new lines in the output.
However adding those to an array of replacements in str_replace still drastically spanked the regular expression replacement.
I feel kinda stupid for overlooking to most obvious solution to a few year old thread haha.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
0
#18 Dec 6th, 2009
This thread is dead folks. Take a look at the last post date.
-1
#19 Dec 6th, 2009
use below script:
The above Script Tested and Worked fine.....
PHP Syntax (Toggle Plain Text)
$string = "This is a test."; $new_string = str_replace (" ", "", $string); echo "$new_string";
The above Script Tested and Worked fine.....
Last edited by hemgoyal_1990; Dec 6th, 2009 at 3:47 am.
http://www.kuchamancity.com
Hem Web Solution..
Behind Every Successful Man, There is an Untold Pain in His Heart.
Hem Web Solution..
Behind Every Successful Man, There is an Untold Pain in His Heart.
![]() |
Similar Threads
- Java's String Tokenizer (Java)
- How to remove spaces from a string (Shell Scripting)
- How to Remove Newline character from a VB String (Visual Basic 4 / 5 / 6)
- DrScheme - Convert a string into a list of words (Computer Science)
- Removing found chars from string (C)
- Removing space from string (Shell Scripting)
- making a string .. name of an object (Java)
- I don't know what to do or to remove *help* (Viruses, Spyware and other Nasties)
Other Threads in the PHP Forum
- Previous Thread: How can I pass large amounts of information through AJAX?
- Next Thread: new com () with notepad possible????
Views: 120096 | Replies: 18
| Thread Tools | Search this Thread |
Tag cloud for PHP
access ajax apache array arrays beginner binary box broken buttons cakephp check checkbox class cms code cookies database date delete directory display download dropdown drupal dynamic echo email error file files form forms function functions header href htaccess html image images include insert ip java javascript joomla jquery limit link list login loop mail menu methods mlm mod_rewrite multiple mysql order output parse password paypal php problem query radio regex remote results script search security select server session sessions soap sort sorting source sql string system table tutorial update upload url user validation validator variable video web website wordpress xml







