strlen() php function giving the wrong length of unicode characters ?



To get the correct length, use mb_strlen() for Unicode characters.

The PHP code is as follows −

Example

 Live Demo

<?php
   $unicodeValues = 'JohnSm?th';
   echo "The string length with mb_strlen=",mb_strlen($unicodeValues, 'utf8');
   echo "
";    echo "The string length with strlen=",strlen($unicodeValues); ?>

Output

The string length with mb_strlen=9
The string length with strlen=10
Updated on: 2020-11-20T05:30:04+05:30

487 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements