php str_split() every n characters with accents

后端 未结 2 1748
盖世英雄少女心
盖世英雄少女心 2020-12-20 05:37

I\'ve this code to split a string every 3 characters, works fine, but accents are messed:

$splitted_array = str_split(\'waderòrò\',3);

but

2条回答
  •  一向
    一向 (楼主)
    2020-12-20 05:51

    I was having the same issue today and here is the solution that I am using. It is basicly using regular expressions.

    $re = '/\w{3}/u';
    $str = 'waderòròцчшщ中华人民共和国';
    
    preg_match_all($re, $str, $matches);
    
    // Print the entire match result
    print_r($matches);
    

提交回复
热议问题