note 48773 deleted from function.substr-replace by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 10:13:59 +0000 |
| Subject: | note 48773 deleted from function.substr-replace by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-196917@lists.php.net to get a copy of this message | ||
Note Submitter: tekrat at 2d dot com
----
Here's a slightly revised version of the truncation function above.
Theres isn't much of a reason to add in the $rep at the end of the original string is less
then the truncation break point.
<?php
function truncate($substring, $max = 50, $rep = '...') {
if(strlen($substring) >= 1){
$string = $substring;
}
$leave = $max - strlen ($rep);
if(strlen($string) > $max){
return substr_replace($string, $rep, $leave);
}else{
return $string;
}
}
?>
[EDIT BY danbrown AT php DOT net: Contains a bugfix by (ogt AT parasane DOT com) (which was me ;-P)
from 21-JAN-05 at 11:02 EST.]