What is the function in php to delete a specified string?

青灯夜游
Release: 2023-03-10 08:46:01
Original
2519 people have browsed it

php function to delete the specified string: 1. str_replace() function, syntax "str_replace(specified string,"", original string)"; 2. preg_replace() function, search by regular expression Specify a string and replace it with a null character.

What is the function in php to delete a specified string?

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

Method 1: str_replace() function

str_replace() function replaces some characters in a string (case sensitive).

str_replace(find,replace,string,count)
Copy after login

This function must follow the following rules:

  • If the searched string is an array, then it will return an array.

  • If the searched string is an array, then it will find and replace each element in the array.

  • If an array needs to be searched and replaced at the same time, and the elements to be replaced are less than the number of found elements, the excess elements will be replaced with empty strings .

  • If you search an array and replace only one string, the replacement string will work for all found values.

Example:

<?php
echo str_replace("world","","Hello world!");
?>
Copy after login

Output:

Hello !
Copy after login

Method 2: preg_replace() function

preg_replace function performs a regular expression search and replace.

Syntax

preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
Copy after login

Search for the part of subject that matches pattern and replace it with replacement.

Parameter description:

  • $pattern: The pattern to be searched, which can be a string or a string array.

  • $replacement: String or array of strings used for replacement.

  • $subject: The target string or string array to be searched and replaced.

  • $limit: Optional, the maximum number of substitutions for each subject string per pattern. The default is -1 (no limit).

  • $count: Optional, the number of times the replacement is performed.

Return value

  • If subject is an array, preg_replace() returns an array, otherwise it returns a string.

If a match is found, the replaced subject is returned, otherwise the unchanged subject is returned. If an error occurs, NULL is returned.

Example:

<?php
$str = &#39;a123abc112233/php.cn/aaccdd321123abcd&#39;;
echo preg_replace("#abc#i", "", $str);
?>
Copy after login

Output:

a123112233/php.cn/aaccdd321123d
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the function in php to delete a specified string?. For more information, please follow other related articles on the PHP Chinese website!

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