Skip to main content
added 4 characters in body
Source Link

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.

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 multibyte support, probably it's better idea to use mb_strlen() & mb_substr(), mb_strpos() rather than strlen(), substr(), mb_strpos() function.

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.

added 50 characters in body
Source Link

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 multibyte support, probably it's better idea to use mb_strlen() & mb_substr(), mb_strpos() rather than strlen(), substr(), mb_strpos() function.

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 multibyte support, probably it's better idea to use mb_strlen() rather than strlen() function.

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 multibyte support, probably it's better idea to use mb_strlen() & mb_substr(), mb_strpos() rather than strlen(), substr(), mb_strpos() function.

Source Link

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 multibyte support, probably it's better idea to use mb_strlen() rather than strlen() function.