Home > Article > Backend Development > How to remove specified string in php
php method to remove the specified string: 1. Use the "$count=strpos($a,"ab");$str=substr_replace($a,"",$count,2);" method Realize string removal; 2. Remove the specified string through the "str_replace" function.
The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer
PHP searches for specified characters in a string The code for concatenating and deleting
PHP searches for the specified string in the string and deletes the code. Search online and it says so, but the actual effect is not that good, or in other words, the effect is not at all Okay
<?php $a = "abcababa"; $count=strpos($a,"ab"); $str=substr_replace($a,"",$count,2); var_dump($str); ?>
The effect is as follows,
OK, this may be the effect you want, but the function that comes with php can perfectly solve this problem
The code is as follows
<?php var_dump(str_replace("ab","","abcaasdfads")); ?>
The official explanation is as follows
Syntax:
str_replace(find,replace,string,count)
Parameters
find: required. Specifies the value to look for.
replace: Required. Specifies the value to replace the value in find.
string :Required. Specifies the string to be searched for.
count : Optional. Variable counting the number of substitutions.
In fact, each has its own merits. The first one is a small algorithm that can limit the number of characters to be replaced, while the second one is to replace all characters. It is a personal choice based on the actual situation.
【Recommended: PHP Video Tutorial】
The above is the detailed content of How to remove specified string in php. For more information, please follow other related articles on the PHP Chinese website!