1

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;
}
  • not sure, but maybe try using the `u` modifier as seen here http://stackoverflow.com/a/1766767/81785 – Moak Nov 21 '12 at 05:06

1 Answers1

1

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ü...
Jacob
  • 974
  • 1
  • 12
  • 21