For PHP beginners, adding, deleting, modifying and checking strings may be a little unfamiliar. In fact, compared to the addition, deletion, modification and query of array elements in mysql database or PHP array, the addition, deletion, modification and query of strings are simpler and easier to understand.
Below we will introduce to you the method of adding, deleting, modifying and checking strings in PHP through a simple code example.
An example of a string code is as follows:
<?php $my_str = ' Hello PHP中文网! '; echo strlen($my_str);
Through the strlen function we can get the length of this string: 23.
1. Add characters to the string:
<?php $my_str = ' Hello PHP中文网! '; $my_str[24]='~ '; echo $my_str;
Add square brackets and numbers after the variable name, which can be expressed as the subscript of the string, and the subscript corresponds to each character in the string. Then since the original length of $my_str is 23, the subscript 24 means adding a new character, and the "~" symbol is added here.
The result is as follows:
2. Delete characters from the string
<?php $my_str = ' Hello PHP中文网! '; $my_str[1]=''; echo $my_str;
Similarly, the character corresponding to the subscript 1 here is H. Reassigning this character to empty means deletion.
The results are as follows:
3. Perform query character operations on the string
<?php $my_str = ' Hello PHP中文网! '; echo $my_str[2];
Here Marked as 2, the query output string is: e.
4. Modify and replace the string:
<?php $my_str = ' Hello PHP中文网! '; $my_str[2]='i'; echo $my_str;
The character with subscript 2 here is e, and we replace it with i.
The results are as follows:
This article is an introduction to the method of adding, deleting, modifying and checking strings in PHP. It is easy to understand. I hope it will be useful to friends who need it. Helped!
The above is the detailed content of How to add, delete, modify and check strings in PHP. For more information, please follow other related articles on the PHP Chinese website!