note 86254 added to function.substr-replace

From: Date: Thu, 09 Oct 2008 16:41:05 +0000
Subject: note 86254 added to function.substr-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-145219@lists.php.net to get a copy of this message
The preemptive test to see if $string is "too long" shouldn't add strlen($replacement) to $max. $max should represent the absolute maximum length of string returned. The size of the $replacement is irrelevant in that determination. The rest of the function (unchanged below) operates as defined above. Meaning, the size of the $replacement is subtracted from the $max, so that the returned string is exactly the length of $max. <?php function truncate($string, $max = 20, $replacement = '') { if (strlen($string) <= $max) { return $string; } $leave = $max - strlen ($replacement); return substr_replace($string, $replacement, $leave); } ?> ---- Server IP: 208.69.120.35 Probable Submitter: 69.223.24.72 ---- Manual Page -- http://www.php.net/manual/en/function.substr-replace.php Edit -- https://master.php.net/note/edit/86254 Del: integrated -- https://master.php.net/note/delete/86254/integrated Del: useless -- https://master.php.net/note/delete/86254/useless Del: bad code -- https://master.php.net/note/delete/86254/bad+code Del: spam -- https://master.php.net/note/delete/86254/spam Del: non-english -- https://master.php.net/note/delete/86254/non-english Del: in docs -- https://master.php.net/note/delete/86254/in+docs Del: other reasons-- https://master.php.net/note/delete/86254 Reject -- https://master.php.net/note/reject/86254 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#145219) next »