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

PHP replaces some characters in a string function str_ireplace()

黄舟
黄舟Original
2017-11-03 10:36:121531browse

Example

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

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

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)
Parameters Description
find Required. Specifies the value to be found.
replace Required. Specifies the value to replace the value in find .
string Required. Specifies the string to be searched for.
count Optional. 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";
?>

实例 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));
?>


Return value: Returns a string with the replacement value or array.
PHP Version: 5+
##Update Log : added the count parameter.

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!

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