Home >Backend Development >PHP Problem >PHP String Learning: How to Calculate the Length of English Strings

PHP String Learning: How to Calculate the Length of English Strings

青灯夜游
青灯夜游Original
2021-08-04 16:42:552985browse

In the previous article, we introduced the method of calculating the length of the array. If you are interested, you can click on the link to view → "PHP Array Learning: Calculating the Array Length of a Two-Dimensional Array". Today we will take a look at strings and learn how to calculate the length of English strings.

There are two main functions used to calculate the length of strings in PHP: strlen() and mb_strlen(). Let's use code examples to understand how the two functions calculate the length of English strings.

1. strlen() function

<?php
header("Content-type:text/html;charset=utf-8");
$str = "hello world!";
echo '字符串 “'.$str.'” 的长度为:'.strlen($str).'<br>';
?>

strlen($string)The function only accepts one parameter$string (String to be calculated), returns 0 if the value passed in this parameter is empty. In the strlen() function, , (comma in English state), numbers, English letters, decimal point ., underline, space, etc. only occupy one character in length.

In the above example, the $str string contains 10 English letters, 1 space and 1 English exclamation mark, a total of 12 characters, so the output result is:

PHP String Learning: How to Calculate the Length of English Strings

But the strlen() function is not suitable for calculating the length of Chinese strings, because in this function, one GB2312-encoded Chinese character occupies two characters in length, and one UTF-8-encoded Chinese character occupies two characters in length. Three characters in length.

2. mb_strlen() function

<?php
header("Content-type:text/html;charset=utf-8");
$str = "hello world!,#%&*@";
echo '字符串 “'.$str.'” 的长度为:'.mb_strlen($str).'<br>';
?>

Similarly, , , numbers, English letters, decimal points in the mb_strlen() function ., underscore, space, #, %, &, *, @ are only Occupies one character in length; but Chinese characters only occupy one character in length.

In the above example, the $str string contains 10 English letters, 1 space, 1 comma, and 6 other English symbols, a total of 18 characters, so the output result is:

PHP String Learning: How to Calculate the Length of English Strings

Summary:

strlen() and mb_strlen() functions can be used to calculate the length of English strings; usually just use strlen(), But sometimes it is a mixed string of Chinese and English, we need to use mb_strlen().

Okay, that’s all. If you want to know anything else, you can click this. → →php video tutorial

Finally, I recommend reading a classic course "PHP String Processing (Jade Girl Heart Sutra Edition)", it's free~ come and learn !

The above is the detailed content of PHP String Learning: How to Calculate the Length of English Strings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn