我正在尝试从给定字符串中删除第一个单词。到目前为止我已经完成了...
$word = 'removeMe|meow|whatever';
$needle = 'removeMe';
$haystack = ''; // To replace with.
$word = str_replace( $needle, $haystack, $word );
它工作得很好,但问题是当$word是这样的...
$word = 'removeMe|meow|removeMe|whatever';
我不想删除第二个$needle。这可能吗?如何实现? )
PHPs 预替换 http://php.net/manual/en/function.preg-replace.php直接通过limit范围
like:
# now: removeMe|meow|removeMe|whatever
$word = preg_replace("/$needle/", $haystack, $word, 1);
# now: |meow|removeMe|whatever
If your needle只出现一开始,某事。就像一个简单的
$word = preg_replace("/^$needle/", $haystack, $word);
应该足够了。
Regards
rbo
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)