How do I get the number of characters in PHP?

mb_strlen

only gives the number of bytes, not what I wanted.

It should work with multibyte characters.

+2


a source to share


7 replies


mb_strlen maybe?



+11


a source


mb_strlen($text, "UTF-8");

      



+9


a source


mb_strlen()

from mb_internal_encoding('UTF-8')

+6


a source


mb_strlen string is measured for length.

<?php
$str = 'abcdef';
echo strlen($str); // 6

$str = ' ab cd ';
echo strlen($str); // 7
?>

      

Directly from the documentation.

0


a source


If you use a UTF-8 encoding step through all bytes in a string and count characters that have the 8th bit NOT set.

This solution does not need the mb extension.

0


a source


I'm not sure about mb_strlen, but I'm just using the old old strlen ... http://php.net/manual/en/function.strlen.php

0


a source


strlen (): Returns the number of bytes, not the number of characters in a string.

$name = "Perú"; // With accent mark
echo strlen($name); // Display 5, because "ú" require 2 bytes.

$name = "Peru"; // Without accent mark
echo strlen($name); // Display 4

      

mb_strlen (): Returns the number of characters in the string containing the character encoding. A multibyte character is considered to be 1.

$name = "Perú"; // With accent mark
echo mb_strlen($name); // Display 4, because "ú" is counted as 1.

$name = "Peru"; // Without accent mark
echo mb_strlen($name); // Display 4

      

iconv_strlen (): Returns the number of characters in a string, as an integer.

$name = "Perú"; // With accent mark
echo iconv_strlen($name); // Display 4.

$name = "Peru"; // Without accent mark
echo iconv_strlen($name); // Display 4

      

0


a source







All Articles