php 如何从右到左分解字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/717328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:37:15  来源:igfitidea点击:

How to Explode String Right to Left?

phpstring

提问by Codex73

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

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? Best simple approach?

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

Basically how do I explode from Right to Left. I want to pick up the last instance of " - ".

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

Result I need:

结果我需要:

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

and not:

并不是:

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

回答by moff

You may use strrevto reverse the string, and then reverse the results back:

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

$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.

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

回答by Paolo Bergantino

How about this:

这个怎么样:

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

Not going to win any golf awards, but it shows intent and works well, I think.

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

回答by Basheer Ahmedu

Here is another way of doing it:

这是另一种方法:

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

$stringpos = strrpos($string, $split_point, -1);
$finalstr = substr($string,0,$stringpos);

回答by v3.

If I understand correctly, you want the example case to give you ('this is my - string', 'and more')?

如果我理解正确,您是否希望示例案例为您提供(“这是我的 - 字符串”、“以及更多”)?

Built-in split/explode seems to be forwards-only - you'll probably have to implement it yourself with strrpos. (right-left search)

内置的拆分/爆炸似乎是仅向前的 - 您可能必须使用 strrpos 自己实现它。(左右搜索)

$idx = strrpos($string, $split_point);
$parts = array(substr($string, 0, $idx), substr($string, $idx+strlen($split_point)))

回答by Alister Bulman

Why not split on ' - ', but then join the first two array entries that you get back together?

为什么不在“ - ”上拆分,然后加入您重新组合在一起的前两个数组条目?

回答by Coomie

I liked Moff's answer, but I improved it by limiting the number of elements to 2 and re-reversing the array:

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

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

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

Then $result will be :

然后 $result 将是:

Array ( [0] => this is my - string [1] => and more )

回答by Deepa MG

this is my - string - and more

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

  1. Use common explode function to get all strings
  2. Get sizeof array and fetch last item
  3. Pop last item using array_popfunction.
  4. Implode remaining string with same delimeter(if u want other delimeter can use).
  1. 使用通用的explode函数获取所有字符串
  2. 获取 sizeof 数组并获取最后一项
  3. 使用array_pop函数弹出最后一项。
  4. 用相同的分隔符内爆剩余的字符串(如果你想要其他分隔符可以使用)。

Code

代码

$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

回答by Dennis

I've stumbled uppon the same, and fixed it like so:

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

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

$reverse_explode = array_reverse(explode($split_point, $string));

回答by John Boe

Just an idea:

只是一个想法:

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);

Result:

结果:

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

But this is much slower then explode. A test made:

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

$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>";

Takes 0.0625 - 0.078125s

需要 0.0625 - 0.078125s

But

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

Takes just 0.015625s

只需要 0.015625s

The fastest solution seems to be:

最快的解决方案似乎是:

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

In a loop of 1000 times this is the best time I can get 0.03125s.

在 1000 次循环中,这是我可以获得 0.03125 秒的最佳时间。

回答by John Boe

$split_point = ' - ';
$string = 'this is my - string - and more';
$result = end(explode($split_point, $string));

This is working fine

这工作正常