Home >Backend Development >PHP Problem >How to delete multiple characters in a string in php

How to delete multiple characters in a string in php

青灯夜游
青灯夜游Original
2021-07-08 16:55:371897browse

In PHP, you can use the substr_replace() function to delete multiple characters in a string. You only need to start from the specified position and replace the specified number of characters with empty characters; the syntax format is "substr_replace(character String,'',starting position,number of replacements)".

How to delete multiple characters in a string in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php Delete string For multiple characters

, you can use the substr_replace() function to replace the specified number of characters with empty characters starting from the specified position.

The code example is as follows:

<?php
$str = &#39;hello,world,hello,world&#39;;
$replace = &#39;&#39;;
echo substr_replace($str, $replace, 0,5);
?>

Output:

,world,hello,world

Explanation:

substr_replace() function replaces part of a string with another string.

The syntax of the substr_replace() function is as follows:

mixed substr_replace ( mixed $string , mixed $replacement , mixed $start [, mixed $length ] )

substr_replace() Replaces the substring qualified by the start and optional length parameters in a copy of the string string using replacement.

If start is a positive number, replacement will start from the start position of string. If start is negative, the replacement will start at the start position from the bottom of string.

If the length parameter is set and is a positive number, it represents the length of the replaced substring in string. If set to a negative number, it represents the number of characters from the end of the substring to be replaced from the end of string. If this parameter is not provided, the default is strlen(string) (the length of the string). Of course, if length is 0, then the function of this function is to insert replacement at the start position of string.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to delete multiple characters in a 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