PHP强制str_replace只替换一个结果
作者:互联网
参见英文答案 > Using str_replace so that it only acts on the first match? 22个
这完美地运作:
$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;
输出:
The _____ of my life I take her home I drive all night to keep her warm and time Is frozen (the _____ of, the _____ of)
我的问题是,是否可以使用内部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)
解决方法:
这可以使用preg_replace来完成.
<?
$str = 'story story story';
echo preg_replace('/story/', '___', $str, 1);
标签:str-replace,php,arrays,string,foreach 来源: https://codeday.me/bug/20190830/1766157.html