Home  >  Article  >  Backend Development  >  What is the php text replacement function?

What is the php text replacement function?

青灯夜游
青灯夜游Original
2021-07-05 18:14:422869browse

In PHP, the text replacement function is str_replace(), which can use a new string to replace a specific string specified in the original string; the syntax format is "str_replace(find value, replace value, string /array,replacement number)".

What is the php text replacement function?

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

In php, text replacement function is str_replace().

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

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 you need to search and replace the array at the same time, and the elements that need to be replaced are less than the number of found elements, the excess elements will be replaced with empty strings

  • If the search is an array and the replacement is a string, then the replacement string will work on all found values.

Syntax

str_replace(find,replace,string,count)
Parameters Description
find Required. Specifies the value to look for.
replace Required. Specifies a value that replaces the value in find.
string Required. Specifies the string to be searched for.
count Optional. Variable counting the number of substitutions.

Return value: Returns a string or array with replacement value.

Example 1:

<?php
$str="Hello world!";
echo str_replace("world","Shanghai",$str);
?>

Output:

Hello Shanghai!

Example 2:

<?php
$find = array("Hello","world");
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_replace($find,$replace,$arr));
?>

Output:

Array ( [0] => B [1] => [2] => ! )

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the php text replacement function?. 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