这个主题:Using str_replace so that it only acts on the first match?现在我需要使用 str_ireplace 函数
答案 0 :(得分:1)
也许设置一个" i"修饰符就足够了:
function str_ireplace_first($from, $to, $subject)
{
$from = '/'.preg_quote($from, '/').'/i';
return preg_replace($from, $to, $subject, 1);
}
echo str_ireplace_first('abc', '123', 'abcdef abcdef abcdef');
(来自@ karim79的答案相同,但在$from
变量中添加了#34; i"