I have a function that truncates texts as follows:
public function truncateText($text, $val)
{
if(strlen($text) > $val){
$content = mb_substr($text, 0, $val) . '...';
return $content;
} else {
return $text;
}
}
I am having issues with multibyte characters. Even though i am using mb_substring i am still getting strange characters at the end of the text. My mb_internal_encoding is UTF-8.
An exemple would be as follows:
The string stored in the database is :
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent malesuada pretium justo, non posuere enim semper vel. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aliquam iaculis nulla velit, eget accumááááá.</p>
When i run this string with the truncateText i get the following with and amp letter at the end:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent malesuada pretium justo, non posuere enim semper vel. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aliquam iaculis nulla velit, eget accum&...
$this->tag->truncateText($text, 250);
However if i test it with the text directly without fetching it from the database and without the
tags then the truncate is good. I tried strip_tags in the string got from the database but still nothing.
var_dump of the mentioned string that is stored in the database:
string(925) "
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent malesuada pretium justo, non posuere enim semper vel. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aliquam iaculis nulla velit, eget accumááááá.
"
var_dump of htmlspecialcharacters:
string(949) "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent malesuada pretium justo, non posuere enim semper vel. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aliquam iaculis nulla velit, eget accumááá.</p> "
What am i missing here?
Many thanks, Trix
echo htmlspecialchars($value)
instead to make a debug output.) – misorude