0

Why is my text not displayed properly when I use "substr"?

function limitstring($input,$number) {
    if (strlen($input) > $number){
        $input = substr($input, 0, $number) . '...';
    } 
    return $input;
}

Input:

echo $row['text'];

Output: Käse

Input:

limitstring($row['text'],60); 

Output: K�se...

peace_love
  • 6,229
  • 11
  • 69
  • 157
  • 1
    Do you mean `substr()` ? Otherwise, if `limitstring()` is a function you've rolled you'll need to show what that's actually doing. – CD001 Jun 26 '17 at 09:59
  • @CD001 Sorry, forgot the function. Will post it in a second – peace_love Jun 26 '17 at 10:01
  • 1
    i don't know limitstring php function, but if you are displaying in a html page, try adding ` ` in the head – Kaddath Jun 26 '17 at 10:02
  • 1
    I think this can help you https://stackoverflow.com/questions/13517189/turkish-characters-are-not-displayed-correctly Good luck – Dalya.R Jun 26 '17 at 10:02
  • @Kaddath Yes, this helped! – peace_love Jun 26 '17 at 10:03
  • *"How can I make substr work with utf-8?"* -- spell "substr" as: [`mb_substr()`](http://php.net/manual/en/function.mb-substr.php). – axiac Jun 26 '17 at 10:09

2 Answers2

2

You should use multibyte set of functions mb_ when dealing with unicode strings. Check here: http://php.net/manual/en/function.mb-substr.php

jakub wrona
  • 2,212
  • 17
  • 17
0

If you can't switch to mb_strlen() and mb_substr() for whatever the reason, which would be the right fix, you need to enable the mbstring.func_overload configuration directive. Also, not all regular byte-based string functions have multi-byte equivalents.

Please note that this is mostly a hack and the directive is deprecated in PHP/7.2.0.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • mbstring.func_overload is not recommended as it can (and often does!) lead to more problems than it solves, especially if you have scripts that have to work with data in more than one character encoding. It is also deprecated and scheduled for removal in the near future – GordonM Jun 26 '17 at 10:07
  • @GordonM Certainly, and that's what I try to express in my answer. – Álvaro González Jun 26 '17 at 10:32