2013-05-17 20 views
13

是否有一種方法可以使用分解函數僅通過最後一個分隔符發生分解?僅以最後一個分隔符分解

$string = "one_two_ ... _three_four"; 

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

結果應該是:

$expoldeResultArray[0] is "one_two_three ..."; 

$expoldeResultArray[1] is "four"; 
+3

怎麼樣'爆炸() '和'implode()'再次排除最後一部分!!! –

回答

39

簡單:

$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" 
+0

我喜歡字符串反轉選項 - 非常容易理解和低CPU。 –

+1

@MichaelCoxon,但使用utf-8字符串會很痛苦。 –

9

你可以做到以下幾點:

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

編輯: array_shift應該已經array_pop

+0

謝謝你的回答! –

+0

array_pop解決了這個問題。 – Alexey

+0

'$ string =「one_two ____ three_four」;'? – xDaizu

1

使用的preg_match()

$string = "one_two_three_four"; 

$arr = array(); 
preg_match("/(^.*)_(.*?)$/", $string, $arr); 

print_r($arr); 

輸出:Array ([0] => one_two_three_four [1] => one_two_three [2] => four)

3
<?php 
$lastPos = strrpos($string, '_'); 
if ($lastPos !== false) { 
    $start = substr($string, 0, $lastPos); 
    $end = substr($string, $lastPos+1); 
} else { 
    // no delimeter found! 
} 

如果你只關心最後一部分,那就更簡單了。

<?php 
$end = substr(strrchr($string, '_'), 1); 
8

我選擇becasue你想要一個字符串到一個特定的點使用字符串:

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

輸出:

six --- five_four_three_two_one 
21

不需要解決方法。 explode()接受否定限制。

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

結果是

one_two_three 
+0

謝謝你的回答! +1 –

+0

'$ string =「one_two ____ three_four」;'? – xDaizu

1
$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 
) 
0

我有類似的需求,並通過@NLZ's answer啓發我做了一個可重複使用的功能與相同的功能,定期explode(),但向後(儘管我補充說編一個選項,以扭轉排列順序反向定期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; 
} 

(Also as a gist.)

所以:

$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 
相關問題