如果使用/ u修饰符,则可以使用特殊的Unicode正则表达式占位符而不是数字范围:
preg_match_all('/
([\p{Han}\p{Katakana}\p{Hiragana}]+) # Kanji
(?: [(] # optional part: paren (
([\p{Hiragana}]+) # Hiragana
[)] )? # closing paren )
\s*=\s* # spaces and =
([\w\s;=]+) # English letters
/ux',
$source, $matches, PREG_SET_ORDER
);
print_r($matches);
我注意到parens中的平假名是可选的,所以我使用(?:…)使你的正则表达式更复杂一些?哪个部分可选.
请注意,结果排序有点不同,因为preg_match_all通常保留index [0]中的完整匹配字符串:
[0] => Array
(
[0] => 時(とき) = toki = time; hour; occasion; moment
[1] => 時
[2] => とき
[3] => toki = time; hour; occasion; moment
)