What is the php text replacement function?

青灯夜游
Release: 2023-03-11 09:10:01
Original
2879 people have browsed it

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)
Copy after login
ParametersDescription
findRequired. Specifies the value to look for.
replaceRequired. Specifies a value that replaces the value in find.
stringRequired. 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);
?>
Copy after login

Output:

Hello Shanghai!
Copy after login

Example 2:

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

Output:

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

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!

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