php 切出每個中文字


最近用來製作模糊搜尋使用的方法,將每個中文字切出後尋找同時有出現這些中文字的資料。

方法

test中文abc會被切成Array ( [0] => test [1] => 中 [2] => 文 [3] => abc ) 存在$matches[0]中

preg_match_all('/([a-zA-Z0-9]+)|\p{Hangul}|\p{Hiragana}|\p{Han}|\p{Katakana}|(\p{Latin}+)|(\p{Cyrillic}+)/u', $str, $matches);

需要其他條件可以在這網站查詢
https://www.regular-expressions.info/unicode.html

參考資料

https://stackoverflow.com/questions/4113802/how-to-split-chinese-characters-in-php

Tags : php