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

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

基本上我如何从右到左爆炸。我只想选择“ - ”的最后一个实例。

结果我需要:

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

并不是:

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

13 回答 13

31

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

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

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

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

于 2009-04-04T16:25:36.063 回答
12

这个怎么样:

$parts = explode($split_point, $string);
$last = array_pop($parts);
$item = array(implode($split_point, $parts), $last);

我认为不会赢得任何高尔夫奖项,但它显示了意图并且效果很好。

于 2009-04-04T16:49:50.157 回答
7

这是另一种方法:

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

$stringpos = strrpos($string, $split_point, -1);
$finalstr = substr($string,0,$stringpos);
于 2012-04-20T09:12:54.530 回答
4

如果我理解正确,您希望示例案例给您('这是我的 - 字符串','等等')?

内置 split/explode 似乎只能向前 - 您可能必须自己使用 strrpos 来实现它。(左右搜索)

$idx = strrpos($string, $split_point);
$parts = array(substr($string, 0, $idx), substr($string, $idx+strlen($split_point)))
于 2009-04-04T16:25:34.880 回答
2

为什么不在' - '上拆分,然后将您重新组合在一起的前两个数组条目连接起来?

于 2009-04-04T16:33:32.207 回答
1

我偶然发现了同样的问题,并像这样修复它:

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

$reverse_explode = array_reverse(explode($split_point, $string));
于 2011-07-15T10:28:46.980 回答
1

我喜欢 Moff 的回答,但我通过将元素数量限制为 2 并重新反转数组来改进它:

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

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

然后 $result 将是:

Array ( [0] => this is my - string [1] => and more )
于 2015-07-09T08:04:50.440 回答
1

这是我的 - 字符串 - 还有更多

  1. 使用常用的explode函数获取所有字符串
  2. 获取 sizeof 数组并获取最后一项
  3. 使用array_pop函数弹出最后一项。
  4. 用相同的分隔符内爆剩余的字符串(如果你想要其他分隔符可以使用)。

代码

$arrSpits=explode("", "this is my - string - and more");
$arrSize=count($arrSpits);

echo "Last string".$arrSpits[arrSize-1];//Output: and more


array_pop(arrSpits); //now pop the last element from array

$firstString=implode("-", arrSpits);
echo "First String".firstString; //Output: this is my - string
于 2017-11-28T07:40:31.717 回答
0

假设您只想忽略 $split_point 的第一次出现,这应该适合您:

# retrieve first $split_point position
$first = strpos($string, $split_point);
# retrieve second $split_point positon
$second = strpos($string, $split_point, $first+strlen($split_point));
# extract from the second $split_point onwards (with $split_point)
$substr = substr($string, $second);

# explode $substr, first element should be empty
$array = explode($split_point, $substr);

# set first element as beginning of string to the second $split_point
$array[0] = substr_replace($string, '', strpos($string, $substr));

这将允许您在第二次出现 $split_point 之后(包括)每次出现 $split_point 时进行拆分。

于 2009-04-05T20:28:11.760 回答
0

只是一个想法:

function explode_reversed($delim,$s){
  $result = array();
  $w = "";
  $l = 0;
  for ($i = strlen($s)-1; $i>=0; $i-- ):
    if ( $s[$i] == "$delim" ):
      $l++;
      $w = "";
    else:
      $w = $s[$i].$w;
    endif;
    $result[$l] = $w;
  endfor;
return $result;
}
$arr = explode_reversed(" ","Hello! My name is John.");
print_r($arr);

结果:

Array
(
    [0] => John.
    [1] => is
    [2] => name
    [3] => My
    [4] => Hello!
)

但这比爆炸要慢得多。做了一个测试:

$start_time = microtime(true);
for ($i=0; $i<1000;$i++)   
  $arr = explode_reversed(" ","Hello! My name is John.");
$time_elapsed_secs = microtime(true) - $start_time;
echo "time: $time_elapsed_secs s<br>";

耗时 0.0625 - 0.078125s

for ($i=0; $i<1000;$i++)   
  $arr = explode(" ","Hello! My name is John.");

仅需 0.015625 秒

最快的解决方案似乎是:

array_reverse(explode($your_delimiter, $your_string));

在 1000 次循环中,这是我能得到 0.03125 秒的最佳时间。

于 2019-10-19T06:14:53.927 回答
0

不知道为什么没有人发布$limit支持的工作功能,尽管这里是为那些知道他们会经常使用它的人准备的:

<?php
function explode_right($boundary, $string, $limit)
{
 return array_reverse(array_map('strrev', explode(strrev($boundary), strrev($string), $limit)));
}

$string = 'apple1orange1banana1cupuacu1cherimoya1mangosteen1durian';
echo $string.'<br /><pre>'.print_r(explode_right(1, $string, 3),1).'</pre>';
?>
于 2020-12-22T05:04:18.180 回答
0

我对所有过度设计的答案感到不知所措,这些答案正在调用许多函数和/或多次迭代数据。所有这些字符串和数组反转功能使该技术非常难以理解。

在这种情况下,正则表达式非常优雅。这正是我在专业应用程序中的做法:

代码:(演示

$string = 'this is my - string - and more';
var_export(preg_split('~.*\K - ~', $string));

输出:

array (
  0 => 'this is my - string',
  1 => 'and more',
)

By greedily matching characters (.*), then restarting the fullstring match (\K), then matching the last occurring delimiting substring (" - "), you are assured to only separate the final substring from the string.

于 2021-03-10T14:05:53.250 回答
-1
$split_point = ' - ';
$string = 'this is my - string - and more';
$result = end(explode($split_point, $string));

这工作正常

于 2014-11-21T09:10:40.800 回答