Home  >  Article  >  Backend Development  >  PHP String Processing Study Notes_PHP Tutorial

PHP String Processing Study Notes_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:16:23825browse

String processing has a large number of character or string processing functions in PHP. Today we will summarize the study notes for PHP string processing. These are not only study notes but also have great benefits in developing applications. .

String in PHP

String variables are used to contain the value of a string.

In this tutorial, we plan to introduce a few of the most commonly used functions and operators used to manipulate strings in PHP.

After creating the string, we can operate on it. You can use the string directly in the 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
 代码如下 复制代码

$txt="Hello World";
echo $txt;
?>

Copy code

$txt="Hello World";
echo $txt;
?>

Output of the above code:

Hello World Now, let’s try using different functions and operators to manipulate our strings.

Concatenation Operator
 代码如下 复制代码

$txt1="Hello World";
$txt2="1234";
echo $txt1 . " " . $txt2;
?>

In PHP, there is only one string operator.

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

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

The code is as follows

Copy code
$txt1="Hello World";
$txt2="1234";
echo $txt1 . " " . $txt2;
?>

 代码如下 复制代码

echo strlen("Hello world!");
?>

Output of the above code:

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

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

Use strlen() function

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

Let’s find the length of the string “Hello world!”:

The code is as follows Copy code
 代码如下 复制代码

echo strpos("Hello world!","world");
?>

echo strlen("Hello world!");
?>

Output of the above code: 12 String length information is often used in loops or other functions, because then it is important to determine when the string ends (for example, in a loop, we need to end after the last character in the string cycle). Use strpos() function The strpos() function is used to retrieve a segment of a string or a character within a string. If a match is found in the string, this function returns the position of the first match. If no match is found, returns FALSE. Let’s try to see if we can 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: 6As you can see, in our string, the string "world" is at position 6. 6 is returned instead of 7 because the first position in the string is 0 instead of 1.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628646.htmlTechArticleString processing There are a large number of character or string processing functions in PHP. Let’s summarize PHP characters for you today. Study notes for string processing. This is not only study notes but also under development...
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