Beispiel #1
0
 /**
  * Splits a string, an array or an ArrayList by one of 4 different methods.
  *      ArrayList (Of any of the 4 object types)
  *      Array (Of any of the 4 object types)
  *      String (To explode by)
  *      Regular Expression (To split by)
  * 
  * This is a powerful tool, but a lot of recursion will occur if using an
  * array and/or an ArrayList.
  * 
  * And 4 basic use cases:
  *      Split a String by a String
  *      Split a String by an Array (nested?) of Strings
  *      Split an Array (nested?) of Strings by a String
  *      Split an Array (nested?) of Strings by an Array (nested?) of Strings
  * 
  * Using Arrays as one of the two parameters can be a tad messy, but easy
  * once you figure it out.
  * 
  * No Matter what an array is returned for each delimiter string and each
  * "split" string, no matter how nested. Using multiple delimiters will not
  * cause the string to be split by multiple delimiters, but instead to split
  * each delimiter into another set of split strings (Refer to line 43).
  * 
  * @param ArrayList|string|array $delimiters The character(s) you're splitting by, can be inside arrays/nested arrays.
  * @param string $string The string you are trying to split.
  * @param bool $allowDuplicates If true then the normal ArrayList "remove duplicate objects" will be ignored, and instead the same value/object may appear in the ArrayList twice.
  * @return ArrayList Split String as an ArrayList, may contain sub-arraylists.
  * @throws Exception
  */
 public static function explode($delimiters, &$string, $allowDuplicates = true)
 {
     if (!is_string($string) && !is_array($string) && !$string instanceof ArrayList) {
         throw new Exception('String is invalid.');
     }
     //$delimiters may be a String (1 delimiter), an ArrayList, an Array or a Regular Expression
     //Array/ArrayList Delimiters
     if ($delimiters instanceof ArrayList) {
         $x = new ArrayList();
         foreach ($delimiters as $delimiter) {
             $x->add($string = ArrayList::explode($delimiter, $string), false, true);
         }
         return $x;
     } else {
         if (is_array($delimiters)) {
             return ArrayList::explode(new ArrayList($delimiters), $string);
             //CHEAP
         } else {
             if (is_string($delimiters)) {
                 if (is_string($string)) {
                     return new ArrayList(explode($delimiters, $string));
                 } else {
                     if (is_array($string)) {
                         return ArrayList::explode($delimiters, new ArrayList($string));
                     } else {
                         if ($string instanceof ArrayList) {
                             $x = new ArrayList();
                             foreach ($string as $str) {
                                 $x->add(ArrayList::explode($delimiters, $str), false, true);
                             }
                             return $x;
                         }
                     }
                 }
             } else {
                 if (@preg_match($delimiters, null) !== false) {
                     //return new ArrayList(preg_split($delimiters, $string));
                 }
             }
         }
     }
     throw new Exception('Invalid Delimiters (Must be Array, ArrayList, Regular Expression or String)');
 }