JpnForPhp\Helper\Helper::split PHP Méthode

split() public static méthode

Enhance default splitter function to handle UTF-8 characters.
public static split ( string $str, integer $length = 1, boolean $yoon = false ) : array
$str string The string to split.
$length integer (optional) Define an optional substring length. Default to 1.
$yoon boolean (optional) Whether considering the base syllable and the following yoon character as a single character or not Default to false.
Résultat array An array of strings.
    public static function split($str, $length = 1, $yoon = false)
    {
        // First split the given string into single characters ; default and
        // most common case.
        $chrs = preg_split("//u", $str, 0, PREG_SPLIT_NO_EMPTY);
        if ($length === 1 && !$yoon) {
            return $chrs;
        }
        // ... handle cases where length != 1
        $str_length = count($chrs);
        $concatChrs = array();
        for ($i = 0, $j = -1, $k = -1; $i < $str_length; $i++) {
            // With yoon set to TRUE, consider the base syllable and the yoon
            // character as a single character.
            $skip = false;
            $k++;
            if ($yoon && preg_match(self::PREG_PATTERN_KANA_YOON, $chrs[$i])) {
                $skip = true;
                $k--;
            }
            if (!$skip && $k % $length === 0) {
                $j++;
                $concatChrs[$j] = $chrs[$i];
            } else {
                $concatChrs[$j] .= $chrs[$i];
            }
        }
        return $concatChrs;
    }

Usage Example

Exemple #1
0
 public function testSplitSequenceOfCharacters()
 {
     $result = Helper::split($this->mixCharacters);
     $this->assertSame(array('今', '日', '、', 'J', 'o', 'o', '「', 'ジ', 'ョ', 'オ', '」', 'は', '学', '校', 'に', 'い', 'ま', 'す', '。'), $result);
 }
All Usage Examples Of JpnForPhp\Helper\Helper::split