How to add, delete, modify and check strings in PHP

藏色散人
Release: 2018-12-12 09:45:48
Original
7143 people have browsed it

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.

How to add, delete, modify and check strings in PHP

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 = &#39; Hello PHP中文网! &#39;;
echo  strlen($my_str);
Copy after login

Through the strlen function we can get the length of this string: 23.

1. Add characters to the string:

<?php
$my_str = &#39; Hello PHP中文网! &#39;;

$my_str[24]=&#39;~ &#39;;
echo $my_str;
Copy after login

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:

How to add, delete, modify and check strings in PHP

2. Delete characters from the string

<?php
$my_str = &#39; Hello PHP中文网! &#39;;

$my_str[1]=&#39;&#39;;
echo $my_str;
Copy after login

Similarly, the character corresponding to the subscript 1 here is H. Reassigning this character to empty means deletion.

The results are as follows:

How to add, delete, modify and check strings in PHP

3. Perform query character operations on the string

<?php
$my_str = &#39; Hello PHP中文网! &#39;;

echo $my_str[2];
Copy after login

Here Marked as 2, the query output string is: e.

4. Modify and replace the string:

<?php
$my_str = &#39; Hello PHP中文网! &#39;;

$my_str[2]=&#39;i&#39;;
echo $my_str;
Copy after login

The character with subscript 2 here is e, and we replace it with i.

The results are as follows:

How to add, delete, modify and check strings in PHP

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template