PHP iconv_strlen() Function
Last Updated :
09 May, 2024
The iconv_strlen() is an inbuilt function in PHP that is used to calculate the length of a string. It is very useful when your working string is encoded in a different character set, as it can accurately determine the length regardless of the encoding.
Syntax:
iconv_strlen(string $string, ?string $encoding = null) : int | false
Parameters:
- $string: The input string from which you want to extract a portion.
- $encoding(optional): This parameter is optional to use. If not provided it will use null.
Return Value:
The return value of the iconv_strlen() function is the length of the string. If this function fails to get the length of the string. It will return false.
Example 1: The following program demonstrates the iconv_strlen() function.
PHP
<?php
$str = "GeeksforGeeks";
$number = iconv_strlen($str) ;
echo "String Length is = ".$number // Output: 9
?>
OutputString Length is = 13
Example 2: The following program demonstrate the iconv_strlen() function. In this program, we will find the length of Chinese chracter and Russian language character.
PHP
<?php
// String with non-ASCII characters (Chinese)
$str1 = "你好,世界!";
// "Hello, World!" in Chinese
// String with non-ASCII characters (Russian)
$str2 = "Привет, мир!";
// "Hello, World!" in Russian
// Calculate lengths using iconv_strlen()
$length1 = iconv_strlen($str1);
$length2 = iconv_strlen($str2);
// Output the lengths
echo "Length of '$str1': $length1\n";
echo "Length of '$str2': $length2\n";
?>
OutputLength of '你好,世界!': 6
Length of 'Привет, мир!': 12