Home  >  Article  >  Backend Development  >  How to delete specified string in PHP

How to delete specified string in PHP

PHPz
PHPzOriginal
2023-03-31 10:07:421167browse

PHP is a very popular programming language that is widely used in the field of web development. In the process of developing web applications, we often need to perform operations on strings, such as intercepting, replacing, deleting, etc. This article will focus on how to delete a specified string in PHP.

In PHP, there are many ways to delete strings. Below we will introduce several of these methods respectively.

1. Use the substr_replace() function

The substr_replace() function is a very practical string operation function in PHP, which can replace a string at a specified position. We can use the substr_replace() function to delete a specific string.

For example, we have a string $mystr = "Hello World! This is a test."; We want to delete the "My" substring, we can use the following code:

$mystr = "Hello World! This is a test.";  
$delete = "My";  
$newstr = substr_replace($mystr,'',strpos($mystr,$delete),strlen($delete));  
echo $newstr;

In this example, we use the substr_replace() function. We first use the strpos() function to obtain the position of the substring to be deleted, and then pass its length to the substr_replace() function to replace the substring to be deleted with an empty string. substring. Eventually, the $newstr variable will get the new string with "My" removed.

2. Use the str_replace() function

The str_replace() function is a very commonly used function in PHP, which can replace a specified string. We can use the str_replace() function to delete a specific string.

For example, we have a string $mystr = "Hello World! This is a test."; We want to delete the "My" substring, we can use the following code:

$mystr = "Hello World! This is a test.";  
$delete = "My";  
$newstr = str_replace($delete,'',$mystr);  
echo $newstr;

In this example, we use the str_replace() function, pass it the value of the substring to be deleted ($delete), replace it with an empty string, and finally get the new string after deleting "My".

3. Use the preg_replace() function

The preg_replace() function is a regular expression function in PHP that can replace text in a specified pattern. We can use the preg_replace() function to delete a specific string.

For example, we have a string $mystr = "Hello World! This is a test."; We want to delete the "My" substring, we can use the following code:

$mystr = "Hello World! This is a test.";  
$delete = "/My/i";  
$newstr = preg_replace($delete,'',$mystr);  
echo $newstr;

In this example, we use the preg_replace() function, passing a regular expression as the first parameter that will match the substring to be removed, and passing an empty string as the second parameter to replace the matched part. Eventually, the $newstr variable will get the new string with "My" removed.

Summary

This article introduces the method of deleting a specified string in PHP, using the substr_replace() function, str_replace() function and preg_replace() function. No matter which method is used, the deletion operation can be implemented. The specific method can be selected according to actual needs and coding habits.

The above is the detailed content of How to delete specified string in PHP. For more information, please follow other related articles on the PHP Chinese website!

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