note 82788 added to function.trim
| From: | davidsteinsland | Date: | Fri, 25 Apr 2008 17:19:25 +0000 |
| Subject: | note 82788 added to function.trim | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-138777@lists.php.net to get a copy of this message | ||
I've noticed that many needs to remove multiple lines in the end of a string, so I made this
function. Pretty easy, actually. It removes all unnecessary newlines (or something else, if $char is
set)
<?php
function remove_unnecessary ($str, $char = "\n") {
while (true) {
if (substr ($str, -(strlen($char))) == $char) {
$str = substr ($str, 0, -(strlen($char)));
} else {
break;
}
}
return $str;
}
$Str = "Hello\nHello\n\n\n\n\n\n\n\n\n";
// Outputs: Hello\nHello
echo remove_unnecessary ($Str);
?>
----
Server IP: 62.73.196.231
Probable Submitter: 85.164.224.129
----
Manual Page -- http://www.php.net/manual/en/function.trim.php
Edit -- https://master.php.net/note/edit/82788
Del: integrated -- https://master.php.net/note/delete/82788/integrated
Del: useless -- https://master.php.net/note/delete/82788/useless
Del: bad code -- https://master.php.net/note/delete/82788/bad+code
Del: spam -- https://master.php.net/note/delete/82788/spam
Del: non-english -- https://master.php.net/note/delete/82788/non-english
Del: in docs -- https://master.php.net/note/delete/82788/in+docs
Del: other reasons-- https://master.php.net/note/delete/82788
Reject -- https://master.php.net/note/reject/82788
Search -- https://master.php.net/manage/user-notes.php