0

I have this code for the title

<?php $thetitle = $post->post_title; $getlength = strlen($thetitle); $thelength = 26; echo substr($thetitle, 0, $thelength); if ($getlength > $thelength) echo "..."; ?>

And this for the excerpt

<?php $content = get_the_content(); $content = strip_tags($content); echo substr($content, 0, 89); ?>...

Everything works correctly, except when the last letter is a special character, because then I get this � (Malformed byte sequence).

1 Answer 1

1

You can use mb_substr(), which is safe to use on multibyte strings. I ran a quick test on my local installation of PHP:

php > $str = 'éééééé';
php > var_dump( substr( $str, 0, 3 ) );
string(3) "é�"
php > var_dump( mb_substr( $str, 0, 3 ) );
string(6) "ééé"
php > var_dump( mb_substr( 'abcdef', 0, 3 );
string(3) "abc"

Not the answer you're looking for? Browse other questions tagged or ask your own question.