Posted By

jamesming on 07/19/08


Tagged

whitespace


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

SpinZ
antpaw
goo
annalog
silentbob84


Converts big whitespaces into one whitespace


 / Published in: PHP
 

URL: http://us3.php.net/trim

  1. <?php
  2. $string = 'This string has no whitespaces.';
  3. echo ereg_replace( ' +', '', $string ); //note: before '+' we have 2 spaces
  4. ?>
  5.  
  6. Output: This string has no white spaces.
  7.  
  8. // OR EVEN BETTER
  9.  
  10. trim's code can of course be simplified with some use of the trim() function....
  11.  
  12. <?php
  13. $str = " Words with lots of spaces ";
  14. $str = preg_replace('/\s\s+/', ' ', trim($str));
  15. ?>
  16.  
  17. Doing the trim() first reduces the workload being put on the more expensive preg_replace().

Report this snippet  

You need to login to post a comment.