note 84728 added to function.substr-replace

From: Date: Sun, 27 Jul 2008 23:14:58 +0000
Subject: note 84728 added to function.substr-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-142827@lists.php.net to get a copy of this message
the version of my predecessor will add $rep even if the string is shorter than max. fixed version: function truncate($string, $max = 20, $rep = '') { if (strlen($string) <= ($max + strlen($rep))) { return $string; } $leave = $max - strlen ($rep); return substr_replace($string, $rep, $leave); } To preserve the filename extension you can call it like this: truncate([filename], 30, '...' . end(explode('.', [filename]))) ---- Server IP: 69.147.83.197 Probable Submitter: 84.189.56.25 ---- Manual Page -- http://www.php.net/manual/en/function.substr-replace.php Edit -- https://master.php.net/note/edit/84728 Del: integrated -- https://master.php.net/note/delete/84728/integrated Del: useless -- https://master.php.net/note/delete/84728/useless Del: bad code -- https://master.php.net/note/delete/84728/bad+code Del: spam -- https://master.php.net/note/delete/84728/spam Del: non-english -- https://master.php.net/note/delete/84728/non-english Del: in docs -- https://master.php.net/note/delete/84728/in+docs Del: other reasons-- https://master.php.net/note/delete/84728 Reject -- https://master.php.net/note/reject/84728 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#142827) next »