title: trim lines and shrink whitespaces using regex for multi line string
body: <p>I<s>'m using a <a href="https://github.com/fvsch/php-trim-whitespace" rel="nofollow noreferrer">php function</a></s> want to create a function to trim all unnecessary white spaces from a multi line string.</p>
  
  <p>The regex that it's not working is the one that removes spaces at the end:</p>
  
  <pre><code>// Always trim at the end. Warning: this seems to be the costlier
  // operation, perhaps because looking ahead is harder?
  $patterns[] = ['/ +$/m', ''];
  </code></pre>
  
  <p>Given the following string from a textarea:</p>
  
  <pre><code> first  line... abc   //&lt;-- blank space here
   second  is  here... def   //&lt;-- blank space here
   //&lt;-- blank space here
   fourth  line... hi  there   //&lt;-- blank space here
  
   sith  is  here....   //&lt;-- blank space here
  </code></pre>
  
  <p>There are blank spaces at the beginning and end of each line plus more than one between the words.</p>
  
  <p>After I run the function:</p>
  
  <pre><code>$functions-&gt;trimWhitespace($description, ['blankLines' =&gt; false]);
  </code></pre>
  
  <p>This is what I get:</p>
  
  <pre><code>first line... abc //&lt;-- blank space here
  second is here... def //&lt;-- blank space here
  //&lt;-- no bank space here
  fourth line... hi there //&lt;-- blank space here
  
  sith is here....//&lt;-- no blank space here
  </code></pre>
  
  <p>Why is it only removing the trailing space from the last line?</p>