Home > Backend Development > PHP Problem > Detailed introduction to the use of PHP replacement function

Detailed introduction to the use of PHP replacement function

PHPz
Release: 2023-04-12 14:28:12
Original
2768 people have browsed it

The PHP replacement function searches for a specified string in a string and replaces all matching strings with another string. Not only can you use these functions to replace strings, you can also manipulate values ​​in arrays. This article will introduce in detail how to use PHP replacement function.

1. PHP replace() function

PHP replace() function is a string replacement function, used to search for another string in a string and replace it with the specified String. The replace() function is very simple to use. You only need to pass three parameters to the function: the string to be found, the string to be replaced, and the string to be operated on. Here is a simple example:

$str = "Hello World!";
echo str_replace("World", "PHP", $str);
?>

In this example, we use the str_replace() function to replace "World" in the string with "PHP". The output is "Hello PHP!", just as we would expect.

2. PHP preg_replace() function

In addition to the str_replace() function, PHP also provides the preg_replace() function, which can use regular expressions to replace strings. Regular expressions are a very powerful tool that can be used to match various complex patterns. The preg_replace() function is similar to the str_replace() function. You only need to pass in the pattern to be matched and the replacement pattern as parameters. Here is a simple example:

$str = "The quick brown fox jumps over the lazy dog.";
echo preg_replace("/\s/", " -", $str);
?>

In this example, we use the preg_replace() function to replace all spaces in the string with "-". The output result is "The-quick-brown-fox-jumps-over-the-lazy-dog."

3. PHP strtr() function

strtr() function is another commonly used The replacement function, which replaces a single character in a string with another character. This function needs to be passed two parameters: the first is the string to be operated on, and the second is an array consisting of old characters and new characters. Here is a simple example:

$str = "Hello World!";
echo strtr($str, "elo", "123");
?>

In this example, we use the strtr() function to replace "e", "l" and "o" in the string with "1", "2" and "3". The output result is "H123 123rld!"

4. PHP str_replace_array() function

The str_replace_array() function is a variant of the str_replace() function, which can accept an array as a search character string and an array as the replacement string. The usage of this function is the same as the str_replace() function. You only need to pass in the string to be operated on, the search string array and the replacement string array. Here is a simple example:

$searchArray = ['World', 'PHP'];
$replaceArray = ['Universe', 'Web Development'];
$str = "Hello World! Learn PHP.";
echo str_replace_array($searchArray, $replaceArray, $str);
?>

In this example, we use The str_replace_array() function replaces "World" in the string with "Universe" and "PHP" with "Web Development". "Hello World! Learn PHP." will be replaced with "Hello Universe! Learn Web Development."

In short, PHP provides a variety of replacement functions, which can be selected and used according to actual needs. Whether you are replacing characters in a string or using regular expressions, PHP provides convenient and simple methods.

The above is the detailed content of Detailed introduction to the use of PHP replacement function. For more information, please follow other related articles on the PHP Chinese website!

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