How to count the length of a post title?

There are actually two problems:

  1. The & is encoded as &, so you have to use html_entity_decode() first.

  2. Multi-byte characters need more than one byte, and strlen() will fail with them. Don’t use strlen().

So use something like this:

$title  = html_entity_decode( get_the_title(), ENT_XML1, 'UTF-8' );
$length = mb_strlen( $title, 'utf-8' );

Leave a Comment

error code: 523