Learning notes for PHP string processing

Source: Internet
Author: User

String processing has a large number of character or string processing functions in php. Let's summarize the learning notes on PHP string processing today, this is not only a learning note, but also has great advantages in developing applications.

PHP Strings

A string variable is used to include the value of a string.

In this tutorial, we will introduce several of the most common functions and operators used to operate strings in PHP.

After creating a string, we can operate it. You can use a string directly in a function or store it in a variable.

Below, the PHP script assigns the string "Hello World" to the string variable named $ txt:

The Code is as follows: Copy code

<? Php
$ Txt = "Hello World ";
Echo $ txt;
?>

Output of the above Code:

Hello World now, let's try to use different functions and operators to operate our strings.

Concatenation Operator)

In PHP, there is only one string operator.

The concatenation operator (.) is used to connect two string values.

To connect two variables together, use the dot operator (.):

The Code is as follows: Copy code

<? Php
$ Txt1 = "Hello World ";
$ Txt2 = "1234 ";
Echo $ txt1. "". $ txt2;
?>

Output of the above Code:

Hello World 1234, you can see that we used the concatenation operator twice in the above example. This is because we need to insert the third string.

To separate the two variables, we insert a space between $ txt1 and $ txt2.

Use strlen () function

The strlen () function is used to calculate the length of a string.

Let's calculate the string "Hello world !" Length:

The Code is as follows: Copy code

<? Php
Echo strlen ("Hello world! ");
?>

Output of the above Code:

12 String Length information is often used in loops or other functions, because it is important to determine when the string ends (for example, in a loop, we need to end the loop after the last character in the string ).

Use the strpos () function

The strpos () function is used to retrieve a string or a character within a string.

If a match is found in the string, the function returns the first matched position. If no matching is found, FALSE is returned.

Let's try to see if the sub-string "world" can be found in the string ":

The Code is as follows: Copy code

<? Php
Echo strpos ("Hello world! "," World ");
?>

The output of the above Code is:

6 As you can see, in our string, the position of the string "world" is 6. 6 instead of 7 is returned because the first position in the string is 0 rather than 1.

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.