PHP 8.5.0 Alpha 4 available for testing

Voting

: two plus six?
(Example: nine)

The Note You're Voting On

kozimbek at mail dot ru
10 years ago
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.

<< Back to user notes page

To Top