php_joe Posted April 29, 2007 Share Posted April 29, 2007 I am trying to write a function that will strip tags and reduce multiple line breaks & tabs to just one. There's probably a better way of doing this, but this is what I've come up with on my own. This strips the tags, breaks each line into an array, separates by tabs, then removes white space. function strip_whitespace($code){ $code = strip_tags($code); $lines = explode("\n", $code); foreach($lines as $lkey => $line){ $pieces = explode("\t", $line); foreach($pieces as $pkey => $piece){ $newpiece[$pkey] = trim($piece); } // end pieces $newline[$lkey] = implode("\t", $newpiece); $newline[$lkey] = trim($newline[$lkey]); } // end lines $output = implode("\n", $newline); return($output); } However, I still end up with double tabs & double line breaks, so I tried this: function strip_whitespace($code){ $code = strip_tags($code); $lines = explode("\n", $code); foreach($lines as $lkey => $line){ $pieces = explode("\t", $line); foreach($pieces as $pkey => $piece){ $newpiece[$pkey] = trim($piece); if(!$newpiece[$pkey]) unset($newpiece[$pkey]); // This reverses the array } // end pieces $newline[$lkey] = implode("\t", $newpiece); $newline[$lkey] = trim($newline[$lkey]); if(!$newline[$lkey]) unset($newline[$lkey]); // but this one works fine } // end lines $output = implode("\n", $newline); return($output); } Why does the $newpiece array order reverse when I unset() the empty values? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.