note 95771 deleted from function.substr by danbrown

From: Date: Thu, 21 Jan 2010 15:25:06 +0000
Subject: note 95771 deleted from function.substr by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-165168@lists.php.net to get a copy of this message
Note Submitter: crubb ---- info /a/ nopticon /dot/ com's word-sensitive _substr method might return strings longer than the length you've specified. Here's a fix: <?php function _substr($str, $length, $minword = 3) { $sub = ''; $len = 0; foreach (explode(' ', $str) as $word) { $part = (($sub != '') ? ' ' : '') . $word; if (strlen($word) > $minword && strlen($sub)+strlen($part) >= $length) { break; } else { $sub .= $part; $len += strlen($part); } } return $sub . (($len < strlen($str)) ? ' ...' : ''); } ?>

« previous php.notes (#165168) next »