gpt4 book ai didi

php - 如何仅在最后出现的分隔符上 explode ?

转载 作者:IT王子 更新时间:2023-10-29 00:09:47 25 4
gpt4 key购买 nike

$split_point = ' - ';
$string = 'this is my - string - and more';

如何使用 $split_point 的第二个实例而不是第一个实例进行拆分。我可以以某种方式指定从右到左的搜索吗?

基本上我如何从右到左 explode 。我只想获取“- ”的最后一个实例。

我需要的结果:

$item[0]='this is my - string';
$item[1]='and more';

而不是:

$item[0]='this is my';
$item[1]='string - and more';

最佳答案

您可以使用 strrev反转字符串,然后将结果反转:

$split_point = ' - ';
$string = 'this is my - string - and more';

$result = array_map('strrev', explode($split_point, strrev($string)));

不确定这是否是最好的解决方案。

关于php - 如何仅在最后出现的分隔符上 explode ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/717328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com