fork download
  1. <?php
  2.  
  3. $text = " first line... abc
  4. second is here... def
  5. <-- blank space here
  6. fourth line... hi there
  7.  
  8. sith is here.... ";
  9.  
  10. // get rid of spaces at the beginning and end of line
  11. $regex = '~^\ +|\ +$~m';
  12. $text = preg_replace($regex, '', $text);
  13.  
  14. // get rid of more than two consecutive spaces
  15. $regex = '~\ {2,}~';
  16. $text = preg_replace($regex, ' ', $text);
  17. echo $text;
  18.  
  19. ?>
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
first line... abc
second is here... def
<-- blank space here
fourth line... hi there

sith is here....