how to remove space from string?

Reply

Join Date: Nov 2007
Posts: 3,833
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 344
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: how to remove space from string?

 
0
  #11
Mar 13th, 2009
Originally Posted by mschroeder View Post
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*
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 151
Reputation: mschroeder is on a distinguished road 
Solved Threads: 25
mschroeder mschroeder is offline Offline
Junior Poster

Re: how to remove space from string?

 
0
  #12
Mar 13th, 2009
Originally Posted by terry.b View Post
Thanks. Very elegant solution. Wish you were around when this question was originally posted.

Oh, and btw, please forgive my ignorance. Like all people who reinvent the so-called wheel, I didn't know it existed. Thanks for the enlightenment.
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,110
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 68
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: how to remove space from string?

 
1
  #13
Mar 15th, 2009
Just to clear things up:

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.
str_replace() will remove *all* occurrence of whitespace.

  1. $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;
}
Offers a solution that removes excess whitespace.

So if you want to turn:

  1. $str = "this is a string with excess whitespace";

into:

  1. "this is a string with excess whitespace";

You could use the StripExtraSpace() function provided above.

Originally Posted by mschroeder View Post
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:
  1. <?php
  2.  
  3. $sTestString = 'This is a stringwith lots of odd spaces and tabs
  4. and some newlines too
  5.  
  6. lets see if this works.';
  7.  
  8. $sPattern = '/\s*/m';
  9. $sReplace = '';
  10.  
  11. echo $sTestString . '<br />';
  12. 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.
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.

  1. function loopStripExtraWhitespaceOptimized($str) {
  2. $len = strlen($str);
  3. for($i = 0; $i < $len; $i++) {
  4. $newstr .= $str[$i];
  5. while($str[$i] == ' ') {
  6. $i++;
  7. }
  8. }
  9. return $newstr;
  10. }

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:

  1. function stripExtraWhitespace($str) {
  2. while($str != ($_str = str_replace(' ', ' ', $str))) {
  3. $str = $_str;
  4. }
  5. return $str;
  6. }

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:

  1. <?php
  2.  
  3. function strStripWhitespace($str) {
  4. return str_replace(' ', '', $str);
  5. }
  6.  
  7. function regexStripExtraWhitespace($str) {
  8. return preg_replace('/\s*/m', ' ', $str);
  9. }
  10.  
  11. function loopStripExtraWhitespace($str) {
  12. $len = strlen($str);
  13. for($i = 0; $i < $len; $i++) {
  14. $newstr .= substr($str, $i, 1);
  15. while(substr($str, $i + 1, 1) == ' ') {
  16. $i++;
  17. }
  18. }
  19. return $newstr;
  20. }
  21.  
  22. function loopStripExtraWhitespaceOptimized($str) {
  23. $len = strlen($str);
  24. for($i = 0; $i < $len; $i++) {
  25. $newstr .= $str[$i];
  26. while($str[$i] == ' ') {
  27. $i++;
  28. }
  29. }
  30. return $newstr;
  31. }
  32.  
  33. function loopStripExtraWhitespace2($str) {
  34. while($str != ($_str = str_replace(' ', ' ', $str))) {
  35. $str = $_str;
  36. }
  37. return $str;
  38. }
  39.  
  40. function benchMark($function, $arg = null, $loops = 1000) {
  41. $start = microtime(1);
  42. for($i = 0; $i < $loops; $i++) {
  43. $function($arg);
  44. }
  45. return microtime(1)-$start;
  46. }
  47.  
  48. ini_set('max_execution_time', 0);
  49.  
  50. $str = "\t hello this is a string.\r\n";
  51. for($i = 0; $i < 10; $i++) {
  52. $str .= $str;
  53. }
  54.  
  55. echo benchMark('strStripWhitespace', $str, 1000);
  56. echo '<br />';
  57. echo benchMark('regexStripExtraWhitespace', $str, 1000);
  58. echo '<br />';
  59. echo benchMark('loopStripExtraWhitespace', $str, 1000);
  60. echo '<br />';
  61. echo benchMark('loopStripExtraWhitespaceOptimized', $str, 1000);
  62. echo '<br />';
  63. echo benchMark('loopStripExtraWhitespace2', $str, 1000);
  64. echo '<br />';
  65.  
  66. ?>
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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 151
Reputation: mschroeder is on a distinguished road 
Solved Threads: 25
mschroeder mschroeder is offline Offline
Junior Poster

Re: how to remove space from string?

 
0
  #14
Mar 15th, 2009
Well you're definitely right. regex is definitely slower when compared to str_replace especially as the length of the string increases. Although 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
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,110
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 68
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: how to remove space from string?

 
0
  #15
Mar 16th, 2009
Originally Posted by mschroeder View Post
Well you're definitely right. regex is definitely slower when compared to str_replace especially as the length of the string increases. Although 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.
Good spotting that one. Yes, it would still be faster with an array of replacements, and even faster if you just did removal of newlines and tabs once outside the function as it doesn't need multiple passes. Only the spaces need multiple passes.
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!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 30
Reputation: elamigosam is an unknown quantity at this point 
Solved Threads: 1
elamigosam elamigosam is offline Offline
Light Poster

Re: how to remove space from string?

 
0
  #16
Jun 12th, 2009
hi I am new here, but I am interested in using that code and I have a question.

I can input a variable, but how can i get the result from it "with no spaces" and put it in a variable, like $nospacename
Reply With Quote Quick reply to this message  
Join Date: Dec 2009
Posts: 1
Reputation: sridharkalabala is an unknown quantity at this point 
Solved Threads: 0
sridharkalabala sridharkalabala is offline Offline
Newbie Poster

Simple way.....!

 
-1
  #17
Dec 6th, 2009
$str=" Your string

with lines

"
$str=str_replace (" ", "", trim($str));
Reply With Quote Quick reply to this message  
Join Date: Feb 2004
Posts: 10,496
Reputation: crunchie is a splendid one to behold crunchie is a splendid one to behold crunchie is a splendid one to behold crunchie is a splendid one to behold crunchie is a splendid one to behold crunchie is a splendid one to behold crunchie is a splendid one to behold 
Solved Threads: 805
Moderator
Featured Poster
crunchie's Avatar
crunchie crunchie is offline Offline
Spyware Killer
 
0
  #18
Dec 6th, 2009
This thread is dead folks. Take a look at the last post date.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 163
Reputation: hemgoyal_1990 is an unknown quantity at this point 
Solved Threads: 11
hemgoyal_1990's Avatar
hemgoyal_1990 hemgoyal_1990 is offline Offline
Junior Poster
 
-1
  #19
Dec 6th, 2009
use below script:

  1. $string = "This is a test.";
  2.  
  3. $new_string = str_replace (" ", "", $string);
  4.  
  5. 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 120096 | Replies: 18
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC