4

How do you trim white space on beginning and the end of each new line with PHP or regex?

For instance,

$text = "similique sunt in culpa qui officia 

     deserunt mollitia animi, id est 

  laborum et dolorum fuga   

";

Should be,

$text = "similique sunt in culpa qui officia

deserunt mollitia animi, id est

laborum et dolorum fuga
";
Run
  • 54,938
  • 169
  • 450
  • 748

6 Answers6

16

Using a regex is certainly quicker for this task. But if you like nesting functions this would also trim whitespace from all lines:

$text = join("\n", array_map("trim", explode("\n", $text)));
mario
  • 144,265
  • 20
  • 237
  • 291
  • 2
    A regex is quicker? I tested my regex (`/[ \t]+$/m`) against your approach (just with `PHP_EOL` instead of "\n") and it is around 5 times slower (22,4ms vs 4,3ms for 1000 iterations over a ~900 char string). And that regex is already two times faster than the other ones here, as it only removes trailing whitespace.. – Dennis98 Apr 07 '17 at 10:30
4
<textarea style="width:600px;height:400px;">
<?php

$text = "similique sunt in culpa qui officia 

     deserunt mollitia animi, id est 

  laborum et dolorum fuga   

";

echo preg_replace("/(^\s+|\s+$)/m","\r\n",$text);

?>
</textarea>

I added the testarea so you can see the newlines clearly

Billy Moon
  • 57,113
  • 24
  • 136
  • 237
  • thanks but I have a problem with this regex with another regex when I have multiple line breaks - `$text = preg_replace("/(\r\n){3,}/","\r\n\r\n",trim($text));` - http://stackoverflow.com/questions/7328404/trim-multiple-line-breaks-and-multiple-spaces-off-a-string – Run Sep 07 '11 at 14:23
  • with your regex, I cannot trim multiple line breaks anymore... how come? – Run Sep 07 '11 at 14:23
  • I think my regex only works with the sample you gave, the linebreaks are lost in my method, then replaced. You are better off using @mario's solution - it is more robust, and probably more efficient. – Billy Moon Sep 07 '11 at 14:30
0

You can use any horizontal whitespace character switch and preg_replace function to replace them with nothing:

$text = trim(preg_replace('/(^\h+|\h+$)/mu', '', $text));

If you need preserve whitespace on file ned remove trim or replace with ltrim function

OzzyCzech
  • 9,713
  • 3
  • 50
  • 34
0

Here another regex that trims whitespace on beginning an end of each line..

$text = preg_replace('/^[\t ]+|[\t ]+$/m', '', $text);

See /m modifier

Pille
  • 1
0

1st solution :

Split your original string with the newline character with explode, use trim on each token, then rebuild the original string ...

2nd solution :

Do a replace with this regular expression \s*\n\s* by \n with preg_replace.

LaGrandMere
  • 10,265
  • 1
  • 33
  • 41
-1

Mario's solution works well (thank you for that mario - saved my day). Maybe somebody can explain though why it turned my later checks for cross-platform compatible line breaks such as

    (strstr($text, PHP_EOL))

to FALSE even when the line breaks existed.

Anyway, changing "\n" to PHP_EOL fixed the issue:

    $text = join(PHP_EOL, array_map("trim", explode(PHP_EOL, $text)));