str_ireplace()

1. 定义

函数str_replace()的忽略大小写版本。 该函数将指定字符串中特定的部分,替换成另一个字符串,不区分大小写。 成功时返回被替换后的结果。

2. 语法

str_ireplace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) : mixed

3. 参数说明

参数 可选性 数据类型 描述
$search 必需 字符串或数组 查找替换的目标
$replace 必需 字符串或数组 替换值
$subject 必需 字符串或数组 将被替换的字符串或数组
&$count 可选 整型 替换发生的次数,只能是变量

$search$replace是数组时,该函数将对$subject做映射替换处理,当$replace数组个数少于$search时,$search的其他部分会被替换为空字符串; 当$search是一个数组而$replace是一个字符串时,$search所有值将被替换为$replace

4. 示例

<?php

// str_ireplace()
// 该函数将指定字符串中特定的部分,替换成另一个字符串,不区分大小写

$search = 'ell';
$replace = '(_replace_)';
$subject = 'hello, Ella. We will see you later on Hangzhou.';
$res = str_ireplace($search, $replace, $subject);
var_dump($res);// 输出:string(63) "h(_replace_)o, (_replace_)a. We will see you later on Hangzhou."

$search = ['Hello','ella','Hangzhou'];
$replace = ['(_replace_1)', '(_replace_2)'];
$res = str_ireplace($search, $replace, $subject);
var_dump($res);// 输出:string(54) "(_replace_1), (_replace_2). We will see you later on ."

5. 延展阅读

  • str_replace():将指定字符串中特定的部分,替换成另一个字符串
  • strtr():将指定字符替换为另一字符。返回被替换后的字符串