How do I get the number of characters in PHP?
7 replies
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 to share
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 to share