How to query the string length in php: first create a new php file and define two strings in the file; then create a new [utf8_strlen()] function and use the [preg_match_all()] method to pass the regular expression Split the string; finally use echo to output the results to the page.
How to query the string length in php:
1. First, create a new php file and name it test. php.
Related learning recommendations: php graphic tutorial
2. In test.php In the file, two strings are defined, a pure English string and the other a mixed Chinese and English string.
3. Use the strlen() method to count the length. It can be seen from the execution program results that the strlen() method counts the string length of $Str1 correctly. The second one is counted. The length of string $Str2 is wrong.
4. Create a new utf8_strlen() function and define its parameter as $string, which defaults to null.
5. Within the utf8_strlen() function, use the preg_match_all() method to split the string through regular expressions and save it in the $match variable. Finally, pass count () method counts the number of array elements obtained and returns the result.
6. Use the utf8_strlen() method to count the lengths of $Str1 and $Str2 strings respectively, and use echo to output the results to the page.
#7. Run the test.php file in the browser and check the results of the program execution. From the two results, it can be seen that the length of the statistical string has been successfully achieved.
Related video recommendations: PHP programming from entry to proficiency
The above is the detailed content of How to query the string length in php. For more information, please follow other related articles on the PHP Chinese website!