user1642018
user1642018

Reputation:

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 ?

Upvotes: 4

Views: 866

Answers (3)

user1642018
user1642018

Reputation:

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>

Upvotes: 0

l&#39;L&#39;l
l&#39;L&#39;l

Reputation: 47159

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

Upvotes: 5

Steini
Steini

Reputation: 2783

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 - "https://www.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.

Upvotes: 1

Related Questions