0

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&ouml;r att passa den &auml;gare som beh&ouml;ver en kompakt, ........ get converted into: Victory har utvecklats f&ouml;r att passa den &a

How can I set it to never cut a string half way through break the html entities?

2 Answers 2

1

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); 
Sign up to request clarification or add additional context in comments.

Comments

1

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" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.