note 49079 added to function.rtrim

From: Date: Sun, 16 Jan 2005 08:49:26 +0000
Subject: note 49079 added to function.rtrim
Groups: php.notes 
Request: Send a blank email to php-notes+get-83398@lists.php.net to get a copy of this message
I needed a way to trim all white space and then a few chosen strings from the end of a string. So I wrote this class to reuse when stuff needs to be trimmed. <?php class cleaner { function cleaner ($cuts,$pinfo) { $ucut = "0"; $lcut = "0"; while ($cuts[$ucut]) { $lcut++; $ucut++; } $lcut = $lcut - 1; $ucut = "0"; $rcut = "0"; $wiy = "start"; while ($wiy) { if ($so) { $ucut = "0"; $rcut = "0"; unset($so); } if (!$cuts[$ucut]) { $so = "restart"; } else { $pinfo = rtrim($pinfo); $bpinfol = strlen($pinfo); $tcut = $cuts[$ucut]; $pinfo = rtrim($pinfo,"$tcut"); $pinfol = strlen($pinfo); if ($bpinfol == $pinfol) { $rcut++; if ($rcut == $lcut) { unset($wiy); } $ucut++; } else { $so = "restart"; } } } $this->cleaner = $pinfo; } } $pinfo = "Well... I'm really bored...<br /><br>&nbsp; \n\t&nbsp;<br><br /><br>&nbsp; \r\r&nbsp;<br>\r<br /><br>\r&nbsp; &nbsp;\n<br> <br />\t"; $cuts = array('\n','\r','\t',' ',' ','&nbsp;','<br />','<br>','<br/>'); $pinfo = new cleaner($cuts,$pinfo); $pinfo = $pinfo->cleaner; print $pinfo; ?> That class will take any string that you put in the $cust array and remove it from the end of the $pinfo string. It's useful for cleaning up comments, articles, or mail that users post to your site, making it so there's no extra blank space or blank lines. ---- Manual Page -- http://www.php.net/manual/en/function.rtrim.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+49079 Delete -- http://master.php.net/manage/user-notes.php?action=delete+49079&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49079&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+49079&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+49079&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+49079&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+49079&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+49079&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#83398) next »