Home > Backend Development > PHP Problem > what is the replace function in php

what is the replace function in php

藏色散人
Release: 2023-03-10 14:32:01
Original
4554 people have browsed it

The replacement function in php is "str_replace". This function is used to replace some characters in a string with other characters. Its usage syntax is "str_replace(find,replace,string,count)".

what is the replace function in php

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

What is the replacement function in php?

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, then the excess elements will be replaced with empty strings

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

Note: This function is case-sensitive. Please use the str_ireplace() function to perform a case-insensitive search.

Note: This function is binary safe.

Syntax

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

Parameters

find Required. Specifies the value to look for.

replace Required. Specifies the value to replace the value in find.

string Required. Specifies the string to be searched for.

count Optional. Variable counting the number of substitutions.

Use str_replace() function with array and count variables:

<?php
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i));
echo "替换数:$i";
?>
Copy after login

Running results:

Array ( [0] => blue [1] => pink [2] => green [3] => yellow ) 替换数:1
Copy after login

Recommended learning: "PHP Video Tutorial

The above is the detailed content of what is the replace function in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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