Reputation: 16823
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?
Upvotes: 0
Views: 109
Reputation: 23354
An alternative approach that it sometimes appropriate, is to truncate at the last space instead. It depends on if you want exactly 255 characters, or if you want something readable, but a useful side-effect is you don't have to worry about HTML entities.
For example:
$string = "this is the long test string to test with";
$limit = 20;
$result = substr($string, 0, strrpos($string, " ", -$limit)-1);
echo $result; // "this is the long"
Upvotes: 1
Reputation: 2972
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...
$long_text = strip_tags(trim($unformatted));
$long_text = html_entity_decode($long_text);
$long_text = mb_strlen($long_text) > $max_length ? mb_substr($long_text, 0, $max_length) : $long_text;
return htmlentities($long_text);
Upvotes: 1