Search code examples
phpsubstrhtmlspecialchars

How to use substring on non english characters?


i have small site where i display search results of posts titles, some titles are up to 255 characters long and while displaying those in html table , the table's row breaks i.e. doesnt shows correct so i use substr php function to trim the title so that it can fit in table row.

For english titles it working great but for non-english titles it shows blank space i.e. trim everything.

i am using substr like this

<a href="<? echo $link; ?>" class="strong"><? echo htmlspecialchars(substr($row['title'],0,70)); ?></a>

so how can i make the non-english titles also of characters 70 ?


Solution

  • You should use multi-byte safe substr() operation based on number of characters for UTF-8:

    mb_substr();
    

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