2012-04-08 265 views
4

我有一個uri,想獲得一個從滿uri血統,直到我只有第一個uri片段。我也有域名,但從最後開始拼接。PHP變換陣列'a','b','c'到'a/b/c','a/b','a'

String 'a/b/c' to array('a/b/c', 'a/b', 'a') 
String 'test.domain.com' to array('test.domain.com', 'domain.com', 'com') 

基於我最初的方法和答案第二種方法和第三種方法我想知道哪一個是最快的,也許還有第四個方法更好?

方法1 :

function explode_special($delimiter, $string, $reverse = false) 
{ 
    if (strpos($string, $delimiter) === false) return array($string); 

    $segments = explode($delimiter, $string); 

    if (! $reverse) 
    { 
     array_walk($segments, function($v, $k, $d) use(&$segments, &$delimiter) { 
      $d[$k] = implode($delimiter, array_slice($segments, 0, (count($segments) - $k))); 
     }, &$parts); 
    } 
    else 
    { 
     array_walk($segments, function($v, $k, $d) use(&$segments, &$delimiter) { 
      $d[$k] = implode($delimiter, array_slice($segments, $k)); 
     }, &$parts); 
    } 

    return $parts; 
} 

方法2:

function explode_special($delimiter, $string, $reverse = false) 
{ 
    if (strpos($string, $delimiter) === false) return array($string); 

    $segments = explode($delimiter, $string); 

    $parts = array(); 

    while ($segments) 
    { 
     $parts[] = implode($delimiter, $segments); 

     if ($reverse) 
     { 
      array_shift($segments); 
     } 
     else 
     { 
      array_pop($segments); 
     } 
    } 

    return $parts; 
} 

方法3:

function explode_special($delimiter, $string, $reverse = false) 
{ 
    if (strpos($string, $delimiter) === false) return array($string); 

    $parts = array($string); 

    for ($i = substr_count($string, $delimiter); $i > 0; $i--) 
    { 
     $parts[] = $string = $reverse 
     // ? trim(strstr($string, $delimiter), $delimiter) 
      ? substr($string, strpos($string, $delimiter) + 1) 
      : substr($string, 0, strrpos($string, $delimiter)); 
    } 

    return $parts; 
} 

輸出:

// explode_special('/', 'a/b/c') = array('a/b/c', 'a/b', 'c'); 
// explode_special('.', 'test.domain.com', true) = 
//  array('test.domain.com', 'domain.com', 'com'); 

回答

1
function multisplit($delim,$str) { 
    $parts = explode($delim,$str); 
    $arr = Array(); 
    while($parts) { 
     $arr[] = implode($delim,$parts); 
     array_pop($parts); 
    } 
    return $arr; 
} 

與該功能,你可以叫multisplit("/","a/b/c");,它將返回['a/b/c','a/b','a']。同樣,請致電multisplit(".","test.domain.com");並獲得['test.domain.com','test.domain','test']

+0

不錯,但什麼域方法是按相反的順序,不test.domain但domain.com – Mike 2012-04-08 14:50:31

+0

只需使用array_reverse然後 – dynamic 2012-04-08 15:10:20

+0

yes123,你有沒有看過我的問題? array_reverse將不起作用 – Mike 2012-04-08 15:13:25

0

我想出了以下

<?php 

header("Content-type: text/plain"); //For display purposes only 

/** 
* Returns an array of strings with diminishing elements of $string according to $delimiter 
* 
* @param string $string  The string 
* @param string $delimiter  The delimiter 
* 
* @return string[]    Array of strings 
*/ 
function degrading_split($string, $delimiter) { 
    $exploded = explode($delimiter, $string); //Array version of the string 
    $result = array();      //Initialize result array 

    foreach ($exploded as $key => $value) {  //Iterate the array 

     $result[] = implode(     #Implode... 
      $delimiter,       #Using the original delimiter 
      array_slice($exploded, $key)  #The complete $exploded array starting from the current $key 
     ); 
    } 

    return $result; 

} 

print_r(degrading_split("a/b/c/d", "/")); 
0

你想想這與正則表達式

function splitString($string,$delim) { 
    $arr = array(); 
    preg_match_all('~['.$delim.']~U',$string,$delimCounter); 
    $arr[] = $string; 
    for ($i = 0;$i<count($delimCounter[0]);$i++) { 
     $string = $arr[] = preg_replace('~^[^.]*['.$delim.']~',"",$string); 
    } 
    return $arr; 
} 
相關問題