Home > Backend Development > PHP Tutorial > PHP replaces some characters in a string function str_ireplace()

PHP replaces some characters in a string function str_ireplace()

黄舟
Release: 2023-03-16 22:58:02
Original
1636 people have browsed it

Example

Replace the character "WORLD" (case-insensitive) in String "Hello world!" with "Peter":

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

Definition And Usage

str_ireplace() function replaces some characters in a string (case insensitive).

This function must follow the following rules:

  • If the string searched 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.

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

Note: This function is binary safe.

Syntax

str_ireplace(find,replace,string,count)
Copy after login
ParametersDescription
find Required. Specifies the value to be found.
replaceRequired. Specifies the value to replace the value in find .
stringRequired. Specifies the string to be searched for.
countOptional. A variable that counts the number of substitutions.

Technical details

In PHP 5.0,

更多实例

实例 1

使用带有数组和 count 变量的 str_ireplace() 函数:

<?php
$arr = array("blue","red","green","yellow");
print_r(str_ireplace("RED","pink",$arr,$i)); // This function is case-insensitive
echo "Replacements: $i";
?>
Copy after login

实例 2

使用带有需要替换的元素少于查找到的元素的 str_ireplace() 函数:

<?php
$find = array("HELLO","WORLD"); // This function is case-insensitive
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_ireplace($find,$replace,$arr));
?>
Copy after login


The above is the detailed content of PHP replaces some characters in a string function str_ireplace(). 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
Return value: Returns a string with the replacement value or array.
PHP Version: 5+
##Update Log : added the count parameter.