According to
In the programming process, string operations are very common. How to delete specified characters or strings from strings is also a very practical skill. In PHP, there are many ways to delete strings, and this article will introduce several of them.
Method 1: str_replace function
The str_replace function can replace the specified character or string with a custom character or string in the string, thereby achieving the purpose of deleting the string. The specific steps are as follows:
Code example:
$string = 'The quick brown fox jumps over the lazy dog.'; $remove = 'o'; $new_string = str_replace($remove, '', $string); echo $new_string;
Output:
The quick brwn fx jumps ver the lazy dg.
In the above code, we use the str_replace function to replace the letter 'o' in the string with the null character string, thereby removing 'o' from the string.
Code example:
$string = 'Hello world!'; $remove = 'wor'; $new_string = str_replace($remove, '', $string); echo $new_string;
Output:
Hello ld!
In the above code, we use the str_replace function nesting to perform the deletion operation, and replace the 'wor in the string ' is replaced with an empty string to implement the deletion operation.
Method 2: substr_replace function
The substr_replace function can replace the character or string at the specified position with a custom character or string, thereby achieving the purpose of deleting the string. The specific steps are as follows:
Code example:
$string = 'The quick brown fox jumps over the lazy dog.'; $start = 20; $length = 5; $new_string = substr_replace($string, '', $start, $length); echo $new_string;
Output:
The quick brown jumps over the lazy dog.
In the above code, we use the substr_replace function to replace the 5 characters starting from the 20th character in the string. character with the empty string, thereby removing 'fox' from the string.
Code example:
$string = 'Hello world!'; $remove = 'world'; $start = strpos($string, $remove); $length = strlen($remove); $new_string = substr_replace($string, '', $start, $length); echo $new_string;
Output:
Hello !
In the above code, we use strpos function and substr function to get the position of 'world' in the string and perform the deletion operation.
Method 3: preg_replace function
The preg_replace function can replace characters or strings that match a specific pattern through regular expression matching, thereby achieving the purpose of deleting strings. The specific steps are as follows:
Code example:
$string = 'The quick brown fox jumps over the lazy dog.'; $remove = '/o/'; $new_string = preg_replace($remove, '', $string); echo $new_string;
Output:
The quick brwn fx jumps ver the lazy dg.
In the above code, we use the preg_replace function to replace the letter 'o' in the string with the null character string, thereby removing 'o' from the string.
Code example:
$string = 'The quick brown fox jumps over the lazy dog.'; $remove = '/br[a-z]+n/'; $new_string = preg_replace($remove, '', $string); echo $new_string;
Output:
The quick fox jumps over the lazy dog.
In the above code, we use the preg_replace function to match strings starting with 'br' and ending with 'n ' ends and contains one or more lowercase letters and replaces it with an empty string to achieve the deletion operation.
Conclusion
Through the introduction of this article, we can see that it is very simple to implement string deletion in PHP. By using the str_replace function, substr_replace function and preg_replace function, we can perform flexible deletion operations according to different needs, which is both simple and practical.
The above is the detailed content of php delete specified number of strings. For more information, please follow other related articles on the PHP Chinese website!