Php 仅按最后一个分隔符分解

Php 仅按最后一个分隔符分解,php,Php,是否有一种方法可以使用explode函数仅按最后一个分隔符的出现进行分解 $string = "one_two_ ... _three_four"; $explodeResultArray = explode("_", $string); 结果应为: echo $explodeResultArray[0]; // "one_two_three ..."; echo $explodeResultArray[1]; // "

是否有一种方法可以使用explode函数仅按最后一个分隔符的出现进行分解

$string = "one_two_  ... _three_four";

$explodeResultArray = explode("_", $string);
结果应为:

echo $explodeResultArray[0]; // "one_two_three ...";
echo $explodeResultArray[1]; // "four";

您可以执行以下操作:

$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
    }
}
编辑: 数组移位应该是数组pop

直接的:

$parts = explode('_', $string);
$last = array_pop($parts);
$parts = array(implode('_', $parts), $last);
echo $parts[0]; // outputs "one_two_three"
正则表达式:

$parts = preg_split('~_(?=[^_]*$)~', $string);
echo $parts[0]; // outputs "one_two_three"
字符串反转:

$reversedParts = explode('_', strrev($string), 2);
echo strrev($reversedParts[0]); // outputs "four"
使用preg_match()


输出:
数组([0]=>one\u two\u three\u four[1]=>one\u two\u three[2]=>four)
我之所以选择使用子字符串,是因为您需要一个到特定点的字符串:

$string = "one_two_three_four_five_six_seven";
$part1 = substr("$string",0, strrpos($string,'_'));
$part2 = substr("$string", (strrpos($string,'_') + 1));
var_dump($part1,$part2);
结果:

string(27) "one_two_three_four_five_six"
string(5) "seven"
输出:

six --- five_four_three_two_one 

没有必要采取变通办法<代码>分解()
接受负限制

$string = "one_two_three_four";
$part   = implode('_', explode('_', $string, -1));
echo $part;
结果是

one_two_three
输出

Array
(
    [0] => one_two_  ... _three
    [1] => four
)

我也有类似的需求,受此启发,我制作了一个可重用的函数,具有与常规函数相同的功能,但功能是向后的(尽管我添加了一个选项,可以将数组顺序反转为反常规的
explode()
):

我们得到:

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
使用结束+爆炸

$explodeResultArray = end(explode("_", $string));

$explodeResultArray将=四个

对于这样的TAK,您只需使用和:


话虽如此,我还是更喜欢正则表达式的答案。

除了最后一部分之外,
explode()
implode()
怎么样!!!我喜欢字符串反转选项-非常简单易懂且CPU占用率低。@MichaelCoxon但使用utf-8字符串可能会很痛苦。要更正语法,请查看为什么正确的语法较短,并且执行完全相同的操作无需添加变量该语句无效。看一下提供的链接中的答案。我不知道,我只是在PHP5.3.3系统上试用过,没有出现错误$hllo=“whater_sfdd”$h=结束(分解(““,$hllo));var_dump(小时);出口结果是sfddI使用xdebug error\u reporting=E\u ALL,这可能就是我得到错误的原因。
one_two_three
$explodeResultArray = explode("_", $string);
$last_item = end($explodeResultArray);
$key = count($explodeResultArray) - 1;
unset($explodeResultArray[$key]);
$arr[] = (implode($explodeResultArray,'_'));
$arr[] = $last_item;
print_r($arr);
Array
(
    [0] => one_two_  ... _three
    [1] => four
)
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;
}
$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));
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
$explodeResultArray = end(explode("_", $string));
$valueBeforeLastUnderscore = rtrim(strrev(strstr(strrev($value), '_')), '_');
$valueAfterLastUnderscore = ltrim(strrchr($value, '_'), '_');