Home >Backend Development >PHP Problem >How to remove specified string in php

How to remove specified string in php

藏色散人
藏色散人Original
2021-03-19 09:18:364604browse

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.

How to remove specified string in php

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,

How to remove specified string in php

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!

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