note 117207 added to function.wordwrap
| From: | kozimbek at mail dot ru | Date: | Sat, 02 May 2015 13:17:52 +0000 |
| Subject: | note 117207 added to function.wordwrap | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-202412@lists.php.net to get a copy of this message | ||
After searching and being tired of many non-working mb_wordwrap functions at many places, I finally
created a really simple and working solution
<?php
function mb_wordwrap($string, $limit)
{
$string = strip_tags($string); //Strip HTML tags off the text
$string = html_entity_decode($string); //Convert HTML special chars into normal text
$string = str_replace(array("\r", "\n"), "", $string); //Also cut
line breaks
if(mb_strlen($string, "UTF-8") <= $limit) return $string; //If input string's
length is no more than cut length, return untouched
$last_space = mb_strrpos(mb_substr($string, 0, $limit, "UTF-8"), " ", 0,
"UTF-8"); //Find the last space symbol position
return mb_substr($string, 0, $last_space, "UTF-8").' ...'; //Return the
string's length substracted till the last space and add three points
}
?>
The function simply searches the last space symbol in the range and returns the string cut till that
position. No iterations, no regular expressions and no buffer overload. Tested with large Russian
texts and works perfectly.
----
Server IP: 72.52.91.14
Probable Submitter: 89.146.83.128
----
Manual Page -- http://php.net/manual/en/function.wordwrap.php
Edit -- https://master.php.net/note/edit/117207
Del: integrated -- https://master.php.net/note/delete/117207/integrated
Del: useless -- https://master.php.net/note/delete/117207/useless
Del: bad code -- https://master.php.net/note/delete/117207/bad+code
Del: spam -- https://master.php.net/note/delete/117207/spam
Del: non-english -- https://master.php.net/note/delete/117207/non-english
Del: in docs -- https://master.php.net/note/delete/117207/in+docs
Del: other reasons-- https://master.php.net/note/delete/117207
Reject -- https://master.php.net/note/reject/117207
Search -- https://master.php.net/manage/user-notes.php