I have the following function to truncate text:
/**
* Removes HTML tags, crops to 255 symbols
*
* @param string $unformatted
*/
public function formatShortDescr($unformatted) {
if(strlen($unformatted)<1) return;
$long_text = strip_tags(trim($unformatted));
$max_length = 255;
if(strlen($long_text) > $max_length){
$short_text = (substr($long_text,0,$max_length));
} else {
$short_text = $long_text;
}
return $short_text;
}
E.g this:
<p>Victory har utvecklats för att passa den ägare som behöver en kompakt, ........ get converted into: Victory har utvecklats för att passa den &a
How can I set it to never cut a string half way through break the html entities?
should be easy to first convert the entities to normal chars, then use mb_strlen (becaus of 2-byte characters, UTF8) to check the length and mb_substring to truncate and THEN convert entities back…