可以用preg_replace完成:
function str_replace_first($from, $to, $content){ $from = '/'.preg_quote($from, '/').'/'; return preg_replace($from, $to, $content, 1);}echo str_replace_first('abc', '123', 'abcdef abcdef abcdef'); // outputs '123def abcdef abcdef'不可思议的地方是可选的第四个参数[Limit]。从文档中:
[限制]-每个主题字符串中每个模式的最大可能替换量。默认为-1(无限制)。



