Search code examples
phpsubstr

How to display part of string if string contains special character


I have following line of code,

echo substr('Sergio Agüero',0,10);

And it will display Sergio Ag�

But I want output like "Sergio Agü"

I don't want special character. So is it possible? Any help is really appreciated.


Solution

  • You can do this using mb_internal_encoding and mb_substr.

    Example: online test

    mb_internal_encoding("UTF-8");
    $str = 'Sergio Agüero';
    echo mb_substr($str, 0, 10); //Sergio Agü
    

    More about: mb-substr