$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';
10 个解决方案
#1
27
You may use strrev to 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.
不确定这是否是最好的解决方案。
#2
12
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.
我认为,不会赢得任何高尔夫奖项,但它显示出意图并且运作良好。
#3
5
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);
#4
4
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)))
#5
2
Why not split on ' - ', but then join the first two array entries that you get back together?
为什么不拆分' - ',然后加入你重新组合的前两个数组条目?
#6
1
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));
#7
1
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 )
#8
1
this is my - string - and more
这是我的 - 字符串 - 还有更多
- Use common explode function to get all strings
- 使用常见的爆炸函数来获取所有字符串
- Get sizeof array and fetch last item
- 获取sizeof数组并获取最后一项
- Pop last item using array_pop function.
- 使用array_pop函数弹出最后一项。
- Implode remaining string with same delimeter(if u want other delimeter can use).
- 使用相同的分隔符内爆剩余字符串(如果你想要其他分隔符可以使用)。
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
#9
0
Assuming you only want the first occurrence of $split_point to be ignored, this should work for you:
假设您只想忽略第一次出现的$ 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));
This will allow you to split on every occurrence of $split_point after (and including) the second occurrence of $split_point.
这将允许您在第二次出现$ split_point之后(包括)拆分$ split_point的每次出现。
#10
0
$split_point = ' - ';
$string = 'this is my - string - and more';
$result = end(explode($split_point, $string));
This is working fine
这工作正常
#1
27
You may use strrev to 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.
不确定这是否是最好的解决方案。
#2
12
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.
我认为,不会赢得任何高尔夫奖项,但它显示出意图并且运作良好。
#3
5
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);
#4
4
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)))
#5
2
Why not split on ' - ', but then join the first two array entries that you get back together?
为什么不拆分' - ',然后加入你重新组合的前两个数组条目?
#6
1
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));
#7
1
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 )
#8
1
this is my - string - and more
这是我的 - 字符串 - 还有更多
- Use common explode function to get all strings
- 使用常见的爆炸函数来获取所有字符串
- Get sizeof array and fetch last item
- 获取sizeof数组并获取最后一项
- Pop last item using array_pop function.
- 使用array_pop函数弹出最后一项。
- Implode remaining string with same delimeter(if u want other delimeter can use).
- 使用相同的分隔符内爆剩余字符串(如果你想要其他分隔符可以使用)。
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
#9
0
Assuming you only want the first occurrence of $split_point to be ignored, this should work for you:
假设您只想忽略第一次出现的$ 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));
This will allow you to split on every occurrence of $split_point after (and including) the second occurrence of $split_point.
这将允许您在第二次出现$ split_point之后(包括)拆分$ split_point的每次出现。
#10
0
$split_point = ' - ';
$string = 'this is my - string - and more';
$result = end(explode($split_point, $string));
This is working fine
这工作正常