Home > Article > Backend Development > How to determine how many characters there are in a string in php
Judgment method: 1. Use the "str_split($str)" statement to convert the string into a character array; 2. Use the "array_unique($arr)" statement to remove duplicate characters in the array; 3. Use " The count (duplication-removed array)" statement counts the length of the array after the duplication is removed, and you can obtain the number of different characters.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php determines how many characters there are in the string , which is to determine which different characters are in the string. Let me introduce to you the judgment method below.
Implementation method:
1. Use the str_split() function to convert the string into a character array
<?php $str = 'absd346d182831'; $arr=str_split($str); var_dump($arr); ?>
2. Use the array_unique() function to remove repeated characters
$newarr=array_unique($arr); var_dump($newarr);
will return a new array, the key value of which is the non-repeating characters in the string .
3. Use the count() function to count the length of the array after deduplication, and you can know the number of different characters in the string.
echo count($newarr);
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to determine how many characters there are in a string in php. For more information, please follow other related articles on the PHP Chinese website!