使用str_reve使它只在第一次匹配时起作用?我想要一个版本的str_replace()的第一次出现。$search在$subject..这是一个简单的解决方案,还是我需要一个笨拙的解决方案?
3 回答
开满天机
TA贡献1786条经验 获得超13个赞
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'
[限制]-每个主题字符串中每个模式的最大可能替换量。默认值为-1(无限制)。
慕尼黑5688855
TA贡献1848条经验 获得超2个赞
$pos = strpos($haystack, $needle);if ($pos !== false) { $newstring = substr_replace($haystack, $replace, $pos, strlen($needle));}
strrpos
strpos
.
- 3 回答
- 0 关注
- 379 浏览
添加回答
举报
0/150
提交
取消