note 78019 added to function.substr

From: Date: Mon, 24 Sep 2007 17:55:03 +0000
Subject: note 78019 added to function.substr
Groups: php.notes 
Request: Send a blank email to php-notes+get-130748@lists.php.net to get a copy of this message
Adding the $limit parameter introduced a bug that was not present in the original. If $limit is small or negative, a string with a length exceeding the limit can be returned. The $limit parameter should be checked. It takes slightly more processing, but it is dwarfed in comparison to the use of strlen(). <?php function short_name($str, $limit) { // Make sure a small or negative limit doesn't cause a negative length for substr(). if ($limit < 3) { $limit = 3; } // Now truncate the string if it is over the limit. if (strlen($str) > $limit) { return substr($str, 0, $limit - 3) . '...'; } else { return $str; } } ?> ---- Server IP: 216.235.15.211 Probable Submitter: 207.194.109.185 ---- Manual Page -- http://www.php.net/manual/en/function.substr.php Edit -- https://master.php.net/note/edit/78019 Del: integrated -- https://master.php.net/note/delete/78019/integrated Del: useless -- https://master.php.net/note/delete/78019/useless Del: bad code -- https://master.php.net/note/delete/78019/bad+code Del: spam -- https://master.php.net/note/delete/78019/spam Del: non-english -- https://master.php.net/note/delete/78019/non-english Del: in docs -- https://master.php.net/note/delete/78019/in+docs Del: other reasons-- https://master.php.net/note/delete/78019 Reject -- https://master.php.net/note/reject/78019 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#130748) next »