public function testSplit() { // ASCII. $res = CRegex::split("He,llo;th,ere!", "/[,;]/"); $this->assertTrue(CArray::length($res) == 4 && CString::equals($res[0], "He") && CString::equals($res[1], "llo") && CString::equals($res[2], "th") && CString::equals($res[3], "ere!")); $res = CRegex::split("He,llo;th.ere!", CArray::fromElements("/[,;]/", "/\\./")); $this->assertTrue(CArray::length($res) == 4 && CString::equals($res[0], "He") && CString::equals($res[1], "llo") && CString::equals($res[2], "th") && CString::equals($res[3], "ere!")); // Special cases. $res = CRegex::split("", "/[,;]/"); $this->assertTrue(CArray::length($res) == 1 && CString::equals($res[0], "")); $res = CRegex::split("Hey", "//"); $this->assertTrue(CArray::length($res) == 3 && CString::equals($res[0], "H") && CString::equals($res[1], "e") && CString::equals($res[2], "y")); $res = CRegex::split("", "//"); $this->assertTrue(CArray::length($res) == 1 && CString::equals($res[0], "")); // Unicode. $res = CRegex::split("¡He,llo·se,ñor!", "/[,·]/u"); $this->assertTrue(CArray::length($res) == 4 && CUString::equals($res[0], "¡He") && CUString::equals($res[1], "llo") && CUString::equals($res[2], "se") && CUString::equals($res[3], "ñor!")); $res = CRegex::split("¡He,llo·se.ñor!", CArray::fromElements("/[,·]/u", "/\\./u")); $this->assertTrue(CArray::length($res) == 4 && CUString::equals($res[0], "¡He") && CUString::equals($res[1], "llo") && CUString::equals($res[2], "se") && CUString::equals($res[3], "ñor!")); // Special cases. $res = CRegex::split("", "/[,·]/u"); $this->assertTrue(CArray::length($res) == 1 && CUString::equals($res[0], "")); $res = CRegex::split("Héy", "//u"); $this->assertTrue(CArray::length($res) == 3 && CUString::equals($res[0], "H") && CUString::equals($res[1], "é") && CUString::equals($res[2], "y")); $res = CRegex::split("", "//u"); $this->assertTrue(CArray::length($res) == 1 && CUString::equals($res[0], "")); }
/** * Splits a string into substrings using a specified regular expression 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|array|map $delimiterPatternOrPatterns The pattern or array of patterns to be recognized as the * delimiter(s). * * @return CArrayObject The resulting strings of type `CUStringObject`. */ public function reSplit($delimiterPatternOrPatterns) { $delimiterPatternOrPatterns = self::ensureUModifier($delimiterPatternOrPatterns); return to_oop(CRegex::split($this, $delimiterPatternOrPatterns)); }