note 82788 deleted from function.trim by hholzgra
| From: | hholzgra@php.net | Date: | Thu, 02 Aug 2012 12:26:55 +0000 |
| Subject: | note 82788 deleted from function.trim by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189659@lists.php.net to get a copy of this message | ||
Note Submitter: davidsteinsland [at] gmail [dot] com
----
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);
?>