Regular expression: Difference between revisions

Jump to navigation Jump to search
Line 354: Line 354:


=== 尋找中文的文字 ===
=== 尋找中文的文字 ===
中文字包含繁體中文與簡體中文。
PHP:
PHP:
<pre>
<pre>
// approach 1
if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $string)) {
if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $string)) {
echo "全部文字都是中文字" . PHP_EOL;
}else{
echo "部分文字不是中文字" . PHP_EOL;
}
// approach 2
if (preg_match('/^[\p{Han}]+$/u', $string)) {
echo "全部文字都是中文字" . PHP_EOL;
echo "全部文字都是中文字" . PHP_EOL;
}else{
}else{
Line 362: Line 371:
}
}
</pre>
</pre>
技術問題除錯:
* 錯誤訊息:<pre>preg_match(): Compilation failed: character value in \x{} or \o{} is too large at offset 8</pre>
解決方式: [http://php.net/manual/en/function.preg-match.php preg_match()] 需要加上 {{kbd | key = u }} 變數<ref>[https://stackoverflow.com/questions/32375531/preg-match-compilation-failed-character-value-in-x-or-o-is-too-large-a php - preg_match(): Compilation failed: character value in \x{} or \o{} is too large at offset 27 on line number 25 - Stack Overflow]</ref>。


參考資料:  
參考資料:  

Navigation menu