3

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 ?

  • 1
    Can you explain more briefly what "non-english-titles" are, do you mean any arabic, kyrillic or chinese letters? – Steini Aug 22 '14 at 05:07

3 Answers3

5

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

l'L'l
  • 44,951
  • 10
  • 95
  • 146
1

http://php.net/manual/de/function.wordwrap.php

That might occur because substr is not multi-byte save function.

You can wether use mb_substr() instead - "http://de1.php.net/manual/de/function.mb-substr.php"

Or try function "wordwrap" because its simply made for cutting strings:

<? echo htmlspecialchars(wordwrap($row['title'], "70", "", true)); ?>

Another possibility is it that this happens when using only htmlspecialchars() without substr()? But this is just a suggestion incase my other two ideas do not help.

Steini
  • 2,753
  • 15
  • 24
0

Try using this custom function i got from DataLife Engine CMS

function dle_substr($str, $start, $length, $charset = "utf-8" ) {

    if ( strtolower($charset) == "utf-8") return iconv_substr($str, $start, $length, "utf-8");
    else return substr($str, $start, $length);

}

like this

<a href="<? echo $link; ?>" class="strong"><? echo  htmlspecialchars(dle_substr($row['title'],0,70)); ?></a>
  • dont use above as iconv_substr is pretty slow , ref : http://evertpot.com/iconv_substr-vs-mbstring_substr/ –  Jun 07 '15 at 05:25