如何仅在最后出现的分隔符上爆炸? [英] How to explode only on the last occurring delimiter?
本文介绍了如何仅在最后出现的分隔符上爆炸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$split_point = ' - ';
$string = 'this is my - string - and more';
如何使用 $ split_point
的第二个实例而不是第一个实例进行拆分.我可以指定从右到左的搜索方式吗?
How can I make a split using the second instance of $split_point
and not the first one. Can I specify somehow a right to left search?
基本上,我如何从右向左爆炸.我只想选择"的最后一个实例-".
Basically how do I explode from right to left. I want to pick up only the last instance of " - ".
我需要的结果:
$item[0]='this is my - string';
$item[1]='and more';
而不是:
$item[0]='this is my';
$item[1]='string - and more';
推荐答案
您可以使用 strrev 进行反向操作字符串,然后将结果反向:
You may use strrev to reverse the string, and then reverse the results back:
$split_point = ' - ';
$string = 'this is my - string - and more';
$result = array_map('strrev', explode($split_point, strrev($string)));
不确定这是否是最好的解决方案.
Not sure if this is the best solution though.
这篇关于如何仅在最后出现的分隔符上爆炸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文