note 54703 added to function.substr-replace

From: Date: Wed, 13 Jul 2005 07:44:44 +0000
Subject: note 54703 added to function.substr-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-91872@lists.php.net to get a copy of this message
If your string is not long enough to meet what you specify in start and length then the replacement string is added towards the end of the string. I wanted to replace the end of the string with ... if the string was too long to display (for instance article preview on a website). The problem was that my string was sometimes not that long and it still added the replacement string. So I wrote a function to replace substr_replace in my website: function add_3dots($string,$repl,$start,$limit) { if(strlen($string) > $limit) { return substr_replace(strip_tags($string),$repl,$start,$limit); } else { return $string; }; }; I use strip_tags to strip out the HTML otherwise you might get a screwed up HTML (when a tags open in the string, but because you cut-off it doesn't) ---- Manual Page -- http://www.php.net/manual/en/function.substr-replace.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54703 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54703&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54703&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54703&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54703&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54703&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54703&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54703&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#91872) next »