Search code examples
phpsubstringsubstr

Fix broken UTF-8 on PHP-substring


I got a little problem:

I wrote my own search engine for my Joomla-based website. Now the problem is, that I generate a preview of the article text using PHP's substring method. Its works fine, but it has some issues when it has to split multibyte-characters, since its not really taking X-Chars, but X-Bytes of the string. This means, that all multibyte characters potentially get splitted by this function, which doesn't look nice.

Anyone know a good workaround but reworking it with additional wordwrap function?

Best wishes


Solution

  • mb_substr will perform a multi-byte safe substring.

    i.e.

    mb_substring('Some string',1,3);
    

    http://php.net/manual/en/function.mb-substr.php