脚本宝典收集整理的这篇文章主要介绍了php – 仅在爆炸匹配的最后一次出现时爆炸字符串,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
<?PHP $string = "one and two and three and four and five"; $string = explode(" and ",$string); PRint_r($string); ?>
结果:
数组([0] =>一[1] =>二[2] =>三[3] =>四[4] =>五)
需要结果:
数组([0] =>一,二,三,四[1] =>五)
$string = "one and two and three and four and five"; $pattern = '/(\w+\s)+/'; preg_match_all($pattern,$string,$matches); $length = strlen($matches[0][0]); echo $matches[0][0]; // 'one and two and three and four and' echo substr($string,$length); // 'five'
这里唯一的问题是第一个匹配仍然有一个尾随’和’,如果需要,可以通过一些简单的编码来摆脱它.如果你想要更复杂的正则表达式,你可以使用积极的前瞻和消极的外观.
以上是脚本宝典为你收集整理的php – 仅在爆炸匹配的最后一次出现时爆炸字符串全部内容,希望文章能够帮你解决php – 仅在爆炸匹配的最后一次出现时爆炸字符串所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。