I am using this code to shorting the title:
function short_title( $after = '', $length ) {
$mytitle = get_the_title();
if( strlen( $mytitle ) > $length ) {
$mytitle = substr( $mytitle, 0, $length );
echo $mytitle . $after;
} else echo $mytitle;
}
and i call it with:
<?php short_title( '...', 40 ); ?>
The script works fine but i am having character problem. See the picture. Any idea how to solve it?
Use
mb_strlen()
, notstrlen()
.The same with
mb_substr()
andsubstr()
: Your title contains multi-byte characters, butstrlen()
andsubstr()
do not work on characters, they work on single bytes.For a improved function to shorten strings see this answer.
as the word is broken at the end, this problem is very likely. you can use the following snippet instead which will finish at word boundary
ref: http://code.web-max.ca/truncate_string.php
however, if you have/use multibyte support, probably it’s better idea to use mb_strlen() & mb_substr(), mb_strpos() rather than strlen(), substr(), mb_strpos() function.