Example #1
0
 /**
  * Splits a string into substrings using a specified pattern or patterns as the delimiter(s) and returns the
  * resulting strings as an array.
  *
  * If no delimiter patterns were found, the resulting array contains just one element, which is the original
  * string. If a delimiter is located at the very start or at the very end of the string or next to another
  * delimiter, it will accordingly cause some string(s) in the resulting array to be empty.
  *
  * As a special case, the delimiter pattern can be empty, which will split the string into its constituting
  * characters.
  *
  * @param  string $string The string to be split.
  * @param  string|array|map $delimiterPatternOrPatterns The pattern or array of patterns to be recognized as the
  * delimiter(s).
  *
  * @return CArray The resulting strings.
  */
 public static function split($string, $delimiterPatternOrPatterns)
 {
     assert('is_cstring($string) && (is_cstring($delimiterPatternOrPatterns) || ' . 'is_collection($delimiterPatternOrPatterns))', vs(isset($this), get_defined_vars()));
     if (is_cstring($delimiterPatternOrPatterns)) {
         $numIdt = self::findGroups($delimiterPatternOrPatterns, "/^([^0-9A-Za-z\\s\\\\])(.*)\\1/", $foundGroups);
         assert('$numIdt == 2', vs(isset($this), get_defined_vars()));
         $idt = $foundGroups[1];
         if (CString::isEmpty($idt)) {
             // Special case.
             if (CString::isEmpty($string)) {
                 $resStrings = CArray::fromElements("");
                 return $resStrings;
             } else {
                 if (preg_match("/^([^0-9A-Za-z\\s\\\\])\\1[A-Za-z]*u[A-Za-z]*\\z/", $delimiterPatternOrPatterns) !== 1) {
                     $resStrings = CArray::make(strlen($string));
                     for ($i = 0; $i < strlen($string); $i++) {
                         $resStrings[$i] = $string[$i];
                     }
                     return $resStrings;
                 } else {
                     return CUString::splitIntoChars($string);
                 }
             }
         }
         $paResStrings = preg_split($delimiterPatternOrPatterns, $string);
         $qty = count($paResStrings);
         $resStrings = CArray::make($qty);
         for ($i = 0; $i < $qty; $i++) {
             $resStrings[$i] = $paResStrings[$i];
         }
         return $resStrings;
     } else {
         $resStrings = CArray::fromElements($string);
         foreach ($delimiterPatternOrPatterns as $delimiterPattern) {
             assert('is_cstring($delimiterPattern)', vs(isset($this), get_defined_vars()));
             $resStringsNew = CArray::make();
             $len = CArray::length($resStrings);
             for ($i = 0; $i < $len; $i++) {
                 CArray::pushArray($resStringsNew, self::split($resStrings[$i], $delimiterPattern));
             }
             $resStrings = $resStringsNew;
         }
         return $resStrings;
     }
 }
Example #2
0
 /**
  * Splits a string into its constituting characters.
  *
  * @return CArrayObject The string's characters of type `CUStringObject`.
  */
 public function splitIntoChars()
 {
     return to_oop(CUString::splitIntoChars($this));
 }