WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 3 down vote accepted

Use mb_strlen(), not strlen().
The same with mb_substr() and substr(): Your title contains multi-byte characters, but strlen() and substr() do not work on characters, they work on single bytes.

For a improved function to shorten strings see this answer.

share|improve this answer
Thanks. works great... – 1907 Jun 17 '12 at 18:21

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

function short_title($after = '', $length)
  $mytitle = get_the_title();
  if (strlen($mytitle) > $length) {
    $mytitle = substr($mytitle, 0, $length);
    $i = strrpos($mytitle, " ");
    $mytitle = substr($mytitle, 0, $i);
    echo $mytitle . $after;
  } else {
    echo $mytitle;

short_title( '...', 40 );

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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