PHP强制str_replace只替换其中一个结果

dav*_*rko 3 php arrays string foreach str-replace

这完美地运作:

$replaceLyrics=str_replace
(
    'story',
    '_____',
    'The story of my life I take her home I drive all night to keep her warm and time Is frozen (the story of, the story of)'
);

echo $replaceLyrics;
Run Code Online (Sandbox Code Playgroud)

输出:

The _____ of my life I take her home I drive all night to keep her warm and time Is frozen (the _____ of, the _____ of)
Run Code Online (Sandbox Code Playgroud)

我的问题是,是否可以使用内部php功能来仅替换匹配中的第一个匹配而不是全部替换.

我期待得到这个输出:

The _____ of my life I take her home I drive all night to keep her warm and time Is frozen (the story of, the story of)
Run Code Online (Sandbox Code Playgroud)

osh*_*ell 5

这可以使用preg_replace来完成.

<?
$str = 'story story story';
echo preg_replace('/story/', '___', $str, 1);
Run Code Online (Sandbox Code Playgroud)