PHP 强制 str_replace 只替换一个结果
PHP force str_replace to replace only one of results
这是完美的工作:
$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);
这是完美的工作:
$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);