note 95771 added to function.substr

From: Date: Thu, 21 Jan 2010 11:48:56 +0000
Subject: note 95771 added to function.substr
Groups: php.notes 
Request: Send a blank email to php-notes+get-165158@lists.php.net to get a copy of this message
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)) ? ' ...' : ''); } ?> ---- Server IP: 85.214.154.209 Probable Submitter: 109.40.139.98 ---- Manual Page -- http://www.php.net/manual/en/function.substr.php Edit -- https://master.php.net/note/edit/95771 Del: integrated -- https://master.php.net/note/delete/95771/integrated Del: useless -- https://master.php.net/note/delete/95771/useless Del: bad code -- https://master.php.net/note/delete/95771/bad+code Del: spam -- https://master.php.net/note/delete/95771/spam Del: non-english -- https://master.php.net/note/delete/95771/non-english Del: in docs -- https://master.php.net/note/delete/95771/in+docs Del: other reasons-- https://master.php.net/note/delete/95771 Reject -- https://master.php.net/note/reject/95771 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#165158) next »