3

Let's say that I have an entry titled Crime & Punishment

I tried to count the number of characters of my entry titles using the following line:

echo strlen( get_the_title() );

I expected the code would give me 18 characters for Crime & Punishment, but the result turned out to be 23 characters.

The code above didn't give me my desired result since it reads the symbols within the title as a HTML code. Is there any workaround for such case?

2 Answers 2

7

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' );
0
0

Try:

<?php echo strip_tags(strlen(get_the_title())); ?>
2
  • unfortunately... that doesn't seem to work.
    – Leader
    May 27, 2014 at 17:44
  • I'm sorry... I'm not sure why, but your code still giving me the same result as what I'm using initially...
    – Leader
    May 27, 2014 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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