note 74024 added to function.substr-replace

From: Date: Wed, 21 Mar 2007 10:00:25 +0000
Subject: note 74024 added to function.substr-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-124657@lists.php.net to get a copy of this message
The two truncate functions provided below, have some major short comings. 1. They may cut a word in half 2. In case you are using htmlentities to get user input or are using wysiwyg editors to get user input, you can get truncated text like "he told C&eac..." instead of "he told Cécile" or "he told". Here is what I use - <?php function truncate($text,$numb) { // source: www.kigoobe.com, please keep this if you are using the function $text = html_entity_decode($text, ENT_QUOTES); if (strlen($text) > $numb) { $text = substr($text, 0, $numb); $text = substr($text,0,strrpos($text," ")); $etc = " ..."; $text = $text.$etc; } $text = htmlentities($text, ENT_QUOTES); return $text; } // Now, to use this function, you can call that as - truncate($text, 75); ?> ---- Server IP: 216.194.113.175 Probable Submitter: 86.69.194.197 ---- Manual Page -- http://www.php.net/manual/en/function.substr-replace.php Edit -- https://master.php.net/note/edit/74024 Del: integrated -- https://master.php.net/note/delete/74024/integrated Del: useless -- https://master.php.net/note/delete/74024/useless Del: bad code -- https://master.php.net/note/delete/74024/bad+code Del: spam -- https://master.php.net/note/delete/74024/spam Del: non-english -- https://master.php.net/note/delete/74024/non-english Del: in docs -- https://master.php.net/note/delete/74024/in+docs Del: other reasons-- https://master.php.net/note/delete/74024 Reject -- https://master.php.net/note/reject/74024 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124657) next »