str_ireplace
str_replace的不区分大小写的版本
PHP 4.0.0 及以上版本
str_ireplace 函数用于在字符串中查找并替换所有出现的子串(不区分大小写)。该函数是 str_replace 函数的大小写不敏感版本。
string str_ireplace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
返回一个经过替换后的字符串,如果提供了数组作为输入,则返回数组形式的替换结果。如果没有进行替换,返回原始的输入字符串。
<?php $input = "Hello world! Welcome to PHP programming."; $output = str_ireplace("hello", "Hi", $input); echo $output; ?>
示例代码说明:这段代码将输入字符串中的 "Hello"(不区分大小写)替换为 "Hi",输出的结果是:Hi world! Welcome to PHP programming.
<?php $input = "This is a test. test test test."; $output = str_ireplace("test", "example", $input, $count); echo $output; // 输出:This is a example. example example example. echo "替换次数:$count"; // 输出:替换次数:4 ?>
示例代码说明:这段代码将所有的 "test"(不区分大小写)替换为 "example",并且通过 $count 变量显示替换发生的次数。