note 70417 added to function.substr

From: Date: Mon, 16 Oct 2006 00:47:50 +0000
Subject: note 70417 added to function.substr
Groups: php.notes 
Request: Send a blank email to php-notes+get-118558@lists.php.net to get a copy of this message
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; } ?> ---- Server IP: 66.207.199.35 Probable Submitter: 70.80.9.195 ---- Manual Page -- http://www.php.net/manual/en/function.substr.php Edit -- https://master.php.net/note/edit/70417 Del: integrated -- https://master.php.net/note/delete/70417/integrated Del: useless -- https://master.php.net/note/delete/70417/useless Del: bad code -- https://master.php.net/note/delete/70417/bad+code Del: spam -- https://master.php.net/note/delete/70417/spam Del: non-english -- https://master.php.net/note/delete/70417/non-english Del: in docs -- https://master.php.net/note/delete/70417/in+docs Del: other reasons-- https://master.php.net/note/delete/70417 Reject -- https://master.php.net/note/reject/70417 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#118558) next »