php 仅按最后一个分隔符展开
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16610791/
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
Explode only by last delimiter
提问by Farid Movsumov
is there a way to use explode function to explode only by last delimiter occurrence?
有没有办法使用爆炸功能仅通过最后一个分隔符出现爆炸?
$string = "one_two_ ... _three_four";
$explodeResultArray = explode("_", $string);
Result Should Be:
结果应该是:
$expoldeResultArray[0] is "one_two_three ...";
$expoldeResultArray[1] is "four";
回答by soulmerge
Straightforward:
直截了当:
$parts = explode('_', $string);
$last = array_pop($parts);
$parts = array(implode('_', $parts), $last);
echo $parts[0]; // outputs "one_two_three"
Regular expressions:
常用表达:
$parts = preg_split('~_(?=[^_]*$)~', $string);
echo $parts[0]; // outputs "one_two_three"
String reverse:
字符串反转:
$reversedParts = explode('_', strrev($string), 2);
echo strrev($reversedParts[0]); // outputs "four"
回答by nibra
There is no need for a workaround. explode()
accepts a negative limit.
不需要解决方法。explode()
接受负限制。
$string = "one_two_three_four";
$part = implode('_', explode('_', $string, -1));
echo $part;
Result is
结果是
one_two_three
回答by Useless Intern
I chose to use substring becasue you want a string up to a particular point:
我选择使用 substring 因为你想要一个字符串到一个特定的点:
$string = "one_two_three_four_five_six_seven";
$part1 = substr("$string",0, strrpos($string,'_'));
$part2 = substr("$string", (strrpos($string,'_') + 1));
var_dump($part1,$part2);
RESULTS:
结果:
string(27) "one_two_three_four_five_six"
string(5) "seven"
回答by NLZ
You could do the following:
您可以执行以下操作:
$string = "one_two_three_four";
$explode = explode('_', $string); // split all parts
$end = '';
$begin = '';
if(count($explode) > 0){
$end = array_pop($explode); // removes the last element, and returns it
if(count($explode) > 0){
$begin = implode('_', $explode); // glue the remaining pieces back together
}
}
EDIT: array_shift should have been array_pop
编辑:array_shift 应该是 array_pop
回答by mcrumley
<?php
$lastPos = strrpos($string, '_');
if ($lastPos !== false) {
$start = substr($string, 0, $lastPos);
$end = substr($string, $lastPos+1);
} else {
// no delimeter found!
}
If you only care about the last part, it's even simpler.
如果你只关心最后一部分,那就更简单了。
<?php
$end = substr(strrchr($string, '_'), 1);
回答by Avi Teller
use end + explode
使用结束+爆炸
$explodeResultArray = end(explode("_", $string));
$explodeResultArray will = four
$explodeResultArray 将 = 4
回答by Robert
Use preg_match()
使用 preg_match()
$string = "one_two_three_four";
$arr = array();
preg_match("/(^.*)_(.*?)$/", $string, $arr);
print_r($arr);
Output:Array ( [0] => one_two_three_four [1] => one_two_three [2] => four )
输出:Array ( [0] => one_two_three_four [1] => one_two_three [2] => four )
回答by michi
// reverse $string right after definition
$string = "one_two_three_four_five_six";
$string = implode("_",array_reverse(explode("_",$string)));
// chop off the first part
list($result, $string) = explode("_", $string, 2);
echo "$result --- $string";
Output:
输出:
six --- five_four_three_two_one
回答by Adem ?zta?
$explodeResultArray = explode("_", $string);
$last_item = end($explodeResultArray);
$key = count($explodeResultArray) - 1;
unset($explodeResultArray[$key]);
$arr[] = (implode($explodeResultArray,'_'));
$arr[] = $last_item;
print_r($arr);
Output
输出
Array
(
[0] => one_two_ ... _three
[1] => four
)
回答by Jari Kein?nen
I had similar needs and inspired by @NLZ's answerI've made a reusable function with the same features as regular explode()
, but backwards (although I added an option to reverse the array order contra regular explode()
):
我有类似的需求并受到@NLZ 回答的启发,我制作了一个可重用的函数,其功能与 regular 相同explode()
,但向后(尽管我添加了一个选项来反转数组顺序 contra regular explode()
):
function backward_explode($delimiter, $string, $limit = null, $keep_order = true) {
if ((string)$delimiter === "") {
return false;
}
if ($limit === 0 || $limit === 1) {
return array($string);
}
$explode = explode($delimiter, $string);
if ($limit === null || $limit === count($explode)) {
return $keep_order? $explode : array_reverse($explode);
}
$parts = array();
if ($limit > 0) {
for ($i = 1; $i < $limit; $i++) {
$parts[] = array_pop($explode);
}
$remainder = implode($delimiter, $explode);
$parts[] = $remainder;
if ($keep_order) {
$parts = array_reverse($parts);
}
} else {
if (strpos($string, $delimiter) === false) {
return array();
}
$parts = $explode;
array_splice($parts, 0, abs($limit));
if (!$keep_order) {
$parts = array_reverse($parts);
}
}
return $parts;
}
So with:
所以用:
$string = 'one two three four';
var_dump(backward_explode(' ', $string));
var_dump(backward_explode(' ', $string, 2));
var_dump(backward_explode(' ', $string, 3));
var_dump(backward_explode(' ', $string, 2, false));
var_dump(backward_explode(' ', $string, -1));
var_dump(backward_explode(' ', $string, 1)); // same as with $limit = 0
var_dump(backward_explode('#', $string, -2));
var_dump(backward_explode('', $string, 3));
We get:
我们得到:
array (size=4)
0 => string 'one' (length=3)
1 => string 'two' (length=3)
2 => string 'three' (length=5)
3 => string 'four' (length=4)
array (size=2)
0 => string 'one two three' (length=13)
1 => string 'four' (length=4)
array (size=3)
0 => string 'one two' (length=7)
1 => string 'three' (length=5)
2 => string 'four' (length=4)
array (size=2)
0 => string 'four' (length=4)
1 => string 'one two three' (length=13)
array (size=3)
0 => string 'two' (length=3)
1 => string 'three' (length=5)
2 => string 'four' (length=4)
array (size=1)
0 => string 'one two three four' (length=18)
array (size=0)
empty
boolean false