String processing in PHP has a large number of characters or string processing functions, we come to you today to summarize the PHP string processing of learning notes, this is not only learning notes at the same time in the development of applications also have a great advantage oh.
A string in PHP
String variables are used to contain the value of a string.
In this tutorial, we are going to introduce several of the most common functions and operators used in PHP to manipulate strings.
After the string has been created, we can manipulate 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 a string variable named $txt:
| The code is as follows |
Copy Code |
$txt = "Hello World"; Echo $txt; ?> |
The output of the above code:
Hello World Now, let's try to manipulate our strings using different functions and operators.
Set operator (concatenation Operator)
In PHP, there is only one string operator.
The collocated operator (.) is used to concatenate two string values together.
To concatenate two variables, use this dot operator (.):
| The code is as follows |
Copy Code |
$txt 1= "Hello World"; $txt 2 = "1234"; echo $txt 1. " " . $txt 2; ?> |
The output of the above code:
Hello World 1234 You can see that we used the two-time collocated operator in the example above. This is because we need to insert a third string.
To separate the two variables, we inserted a space between $txt 1 and $txt 2.
Use the strlen () function
The strlen () function is used to calculate the length of a string.
Let's figure out the length of the string "Hello world!":
| The code is as follows |
Copy Code |
Echo strlen ("Hello world!"); ?> |
The 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 matching position. If no match is found, FALSE is returned.
Let's try to find the substring "world" in the string:
| The code is as follows |
Copy Code |
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. Returns 6 instead of 7 because the first position in the string is 0, not 1.
http://www.bkjia.com/PHPjc/628646.html www.bkjia.com true http://www.bkjia.com/PHPjc/628646.html techarticle string processing in PHP has a large number of characters or string processing functions, we come to you today to summarize the PHP string processing of learning notes, this is not only learning notes at the same time in the development ...