Convert a String into an Array of Characters - multi-byte

前端 未结 2 609
刺人心
刺人心 2020-12-07 02:48

Assuming that in 2019 every solution which is not UNICODE-safe is wrong. What is the best way to convert a string to array of UNICODE characters in PHP?

Obviously th

2条回答
  •  旧时难觅i
    2020-12-07 03:48

    This works for me, it explodes a unicode string into an array of characters:

    //
    // split at all position not after the start: ^
    // and not before the end: $, with unicode modifier
    // u (PCRE_UTF8).
    //
    $arr = preg_split("/(?

    For example:

    
    
    
    
    
    ';
    
    print_r($arr);
    
    echo '
    
    ';
    ?>
    

    In a browser, it produces this:

    Array ( [0] => 堆 [1] => 栈 [2] => 溢 [3] => 出 )
    

提交回复
热议问题