Home > Backend Development > PHP Tutorial > PHP code to find a specified string in a string and delete it

PHP code to find a specified string in a string and delete it

WBOY
Release: 2016-07-29 08:58:50
Original
838 people have browsed it

PHP searches for the code for specifying a string in a string and deletes it. Searching the Internet says this, but the actual effect is not that good, or in other words, the effect is not good at all

<code><span><span><?php </span><span>$a</span> = <span>"abcababa"</span>; 
<span>$count</span>=strpos(<span>$a</span>,<span>"ab"</span>); 
<span>$str</span>=substr_replace(<span>$a</span>,<span>""</span>,<span>$count</span>,<span>2</span>);
var_dump(<span>$str</span>);
<span>?></span></span></span></code>
Copy after login

The effect is as follows,
PHP code to find a specified string in a string and delete it
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

<code><span><span><?php </span>
var_dump(str_replace(<span>"ab"</span>,<span>""</span>,<span>"abcaasdfads"</span>));
<span>?></span></span></span></code>
Copy after login

The official explanation is as follows
Grammar:
str_replace(find,replace,string,count)
Parameter Description
find: required. Specifies the value to be found.
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 strengths. 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.

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces the code for PHP to find a specified string in a string and delete it, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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