In some website registration or other activities, users need to submit some information, which may have certain requirements. for example, the user registration information must be limited to a maximum length. Here, we need to use the built-in function of PHP for obtaining operations. In some website registration or other activities, users need to submit some information, which may have certain requirements. for example, the user registration information must be limited to a maximum length. Here, we need to use the built-in function of PHP for obtaining operations.
The strlen () function is used to obtain the string length.The syntax and application of strlen () function.
Strlen () functionIt is mainly used to obtain the length of a specified string.
The syntax format is as follows:
strlen(string);
Parameter: string to calculate the length.
Return value: generally the length of the returned string. if the string is null, the return value is 0.
Use the strlen () function to obtain the string length. the sample code is as follows:
'; // The length is 19?>
Output result:19
Result analysis:
When the strlen character length is used, a UTF-8 Chinese character is counted as three characters in length. Numbers, English letters, decimal points, underscores, and spaces occupy one character, therefore, the length of "character a character 1 string B length 2 degrees" is 3 + 1 + 3 + 1 + 3 + 1 + 3 + 1 + 3 = 19.
Note that in gb2312, when strlen calculates the character length, only two Chinese characters are counted.
In addition to obtaining the string length, the strlen () function can also be used to detect the string length.
The following uses a simple exampleUse the strlen () function to check the length of the submitted user passwordIf the password length is less than 5, a prompt is displayed.
The procedure is as follows:
1. create a php file named index. php.
2. add a form and set the action attribute of the form to test. php.
3. design a form using the html tag. add a user name text box named user. add a password text box named pwd.
4. create another php file named test. php and write the following code:
In the above code, the POST method (the POST method can be viewed in the PHP Chinese network) is used to receive the user password string entered by the user. Use the strlen () function to obtain the length of the password entered by the user, and use the if condition control statement to determine the length of the password. if the length of the password entered by the user does not reach the specified length, A prompt box is displayed.
The above is the detailed description of the strlen () function used by PHP to obtain the string length function. For more information, see other related articles in the first PHP community!