user1149162
user1149162

Reputation: 27

Display complete umlaut with PHP text limit function

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;
}

Upvotes: 1

Views: 677

Answers (1)

Jacob
Jacob

Reputation: 1004

It seems that you are working with UTF-8 strings and both strlen, substr and your preg_match isn'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):

function textLimit($string, $length, $replacer = '...') {
  if(mb_strlen($string) > $length) {
    return (preg_match('/^(.*)\W.*$/u', mb_substr($string, 0, $length+1), $matches) ? $matches[1] : mb_substr($string, 0, $length)) . $replacer;
  }

  return $string;
}

mb_internal_encoding('UTF-8');
echo 'example: '.textLimit('füüür', 2)."\n";

Outputs:

example: fü...

Upvotes: 1

Related Questions