如何用 php 替换子字符串,无论它是小字符还是大字符


How to replace substring with php whatever it is lower or upper characters

我需要替换子字符串,无论这个字符串大写还是小写

$sub = "STR";
$newsub="All-";
$str="Strstring";
$newstr=str_replace($sub,$rep,$str);

你可以使用 PHP 的str_ireplace函数作为

$sub = "STR";
$newsub="All-";
$str="Strstring";
$newstr=str_ireplace($sub,$newsub,$str);

小提琴

你也可以

使用preg_replace -

$sub = array('/STR/i');
$newsub="All-";
$str="Strstring";
echo $newstr=preg_replace($sub, $newsub, $str);

输出

All-All-ing