I have the following function that replaces strings with more than 32 characters with “…”. My problem is that if an umlaut falls in the range of 29th – 33rd character, it is represented by a weird character. How do I alter the function to show the entire umlaut without breaking it, despite what number length I place in the variable $length?
For example, there are 31 characters ahead of für, but using the function below, it gives 31 characters plus f�…
function textLimit($string, $length, $replacer = '...')
{
if(strlen($string) > $length)
return (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer;
return $string;
}
It seems that you are working with UTF-8 strings and both
strlen,substrand yourpreg_matchisn’t aware of this. For the string functions you need to use the this:http://www.php.net/manual/en/ref.mbstring.php
The following example should work with UTF-8 strings (notice the mb functions and the u preg modifier):
Outputs: