note 88840 deleted from function.strlen by danbrown
| From: | danbrown@php.net | Date: | Thu, 12 Feb 2009 03:11:23 +0000 |
| Subject: | note 88840 deleted from function.strlen by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-150530@lists.php.net to get a copy of this message | ||
Note Submitter: n dot rijksen at gmail dot com
----
Small notice on "kyle dot florence ~at~ gmail dot com" his function.
The way his function is now the substr would be performed on a html string and THEN convert
it's html entities.. whilst it should convert the html entities before performing the substr in
order to get the expected results..
The correct code would be;
<?php
// Trim a string to specified length and append an end character (default = ...)
function str_trim($str, $lim = 32, $chr = '…')
{
// If length of string is less than $lim, return string
if (strlen($str = htmlentities($str)) <= $lim) return $str;
// Else, cut string down to size
return substr(htmlentities($str), 0, $lim - 3).$chr;
}
?>