note 48773 modified in function.substr-replace by danbrown

From: Date: Sat, 14 Feb 2009 06:23:39 +0000
Subject: note 48773 modified in function.substr-replace by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-150668@lists.php.net to get a copy of this message
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.] --was-- 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. <? function truncate($substring, $max = 50, $rep = '...') { if(strlen($substring) < 1){ $string = $rep; }else{ $string = $substring; } $leave = $max - strlen ($rep); if(strlen($string) > $max){ return substr_replace($string, $rep, $leave); }else{ return $string; } } ?> http://php.net/manual/en/function.substr-replace.php

« previous php.notes (#150668) next »