note 70417 deleted from function.substr by cmb
| From: | cmb@php.net | Date: | Sun, 27 Oct 2019 09:06:10 +0000 |
| Subject: | note 70417 deleted from function.substr by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212822@lists.php.net to get a copy of this message | ||
Note Submitter: feedback at realitymedias dot com
----
This function can replace substr() in some situations you don't want to cut right in the middle
of a word. strtrim will cut between words when it is possible choosing the closest possible final
string len to return. the maxoverflow parameter lets you choose how many characters can overflow
past the maxlen parameter.
<?php
function strtrim($str, $maxlen=100, $elli=NULL, $maxoverflow=15) {
global $CONF;
if (strlen($str) > $maxlen) {
if ($CONF["BODY_TRIM_METHOD_STRLEN"]) {
return substr($str, 0, $maxlen);
}
$output = NULL;
$body = explode(" ", $str);
$body_count = count($body);
$i=0;
do {
$output .= $body[$i]." ";
$thisLen = strlen($output);
$cycle = ($thisLen < $maxlen && $i < $body_count-1 &&
($thisLen+strlen($body[$i+1])) < $maxlen+$maxoverflow?true:false);
$i++;
} while ($cycle);
return $output.$elli;
}
else return $str;
}
?>