PHP Get string length function strlen () function

Source: Internet
Author: User
In some Web site registration or other activities, we need users to submit some information, for this information will have certain requirements, such as user registration information Some of the limit length can not be less than how much. Here we need to use PHP's own function to get the operation.

getting the string length is using the strlen () function , which says the syntax and application of the strlen () function.

The strlen () function is primarily used to get the length of a string of the specified strings.

The syntax format is as follows:

Strlen (string);

Parameter: String that needs to be evaluated for length.

Return value: Typically the length of the returned string, or a value of 0 if the string is empty.

Use the strlen () function to get the length of the string, the sample code is as follows:

<?php   $str = ' character a character 1 string B length 2 degrees ';    echo strlen ($str). ' <br> ';    Length is 19?>

The output is:

Results Analysis:

In the case of strlen character length, a UTF8 character is counted as 3 lengths, Numbers, English, decimal points, underscores, and spaces are the characters, so the "character a character 1 string B length 2 degrees" is 3+1+3+1+3+1+3+1+3=19.

It is important to note that in gb2312, a Chinese character is only 2 lengths when Strlen calculates the length of the characters.

The strlen () function can also be used to detect the length of a string in addition to the length of the string.

The following is a simple example of using the strlen () function to detect the length of a committed user password , if the password length is less than 5, a message will pop up.

The steps to do this are as follows:

1. Create a new PHP file named index.php.

2. Add a form to set the form's Action property to test.php.

3. Apply HTML tags to design a form, add a username text box named user, and add a password text box named PWD.


4. Also create a php file, named test.php, which writes the following code:

<?php if (strlen ($_post[' pwd ") <6)         //test if the user's password is less than 5 {      //does not meet the requirements then pops up the message    echo" <script>alert (' Password cannot be less than 5 bit! Please re-enter '); History.back ();</script> "; }?>

The

In the preceding code is the user password string that is received by the user input through the Post method (for the Post method can be viewed in topic.alibabacloud.com). Through the strlen () function to obtain the length of the user input password, and using the IF condition control statement to determine the length of the user password, if the user entered a password does not reach the specified length, it will pop up the prompt box information.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.