Function that trims whitespace and ads punctuation if there is none
<p>I am trying to make a function that cuts the white spaces in a string and ads punctuation if there isn't any.</p>

<p>This is my test string:</p>

<pre><code>$test = 'Hey there I am traa la la           ';
</code></pre>

<p>I want it to turn into this:</p>

<pre><code>$test = 'Hey there I am traa la la.';
</code></pre>

<p>Here is the function I have tried:</p>

<pre><code>function test($mytrim){
        for($i = 0; $i &lt;= 5; $i++){
            if(substr($mytrim, 0, -1) == ''){
                $mytrim = substr($mytrim, 0, -1);
            }
        }
        $punct = array(".",",","?","!");
        if(!in_array($mytrim, $punct)){ $mytrim .= '.'; } 
        return $mytrim;
    }
</code></pre>

<p>It returns this:</p>

<pre><code>$mytrim = 'Hey there I am traa la la. .';
</code></pre>

<p>Any ideas why it is not working?</p>
