Editing
Regular expression in Mandarin
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== Find non-ASCII characters in PHP ==== 尋找欄位值包含中文字,中文字包含繁體中文與簡體中文,不包含標點符號 (例如 {{kbd | key = <nowiki>,</nowiki>}})、全形標點符號 (例如 {{kbd | key = <nowiki>,</nowiki>}})以及特殊符號,例如 Emoji:{{kbd | key = ⭐}}。 PHP: exact match <pre> // approach 1 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; }else{ echo "部分文字不是中文字" . PHP_EOL; } </pre> partial match ([http://sandbox.onlinephpfunctions.com/code/d780845d20877c0fd2e693b28ed02a10d250d39e online demo] hosted by [http://sandbox.onlinephpfunctions.com/ PHP Sandbox]) <pre> // approach 1 $string = '繁體中文-简体中文-English-12345-。,!-.,!-⭐'; $pattern = '/[\p{Han}]+/u'; preg_match_all($pattern, $string, $matches, PREG_OFFSET_CAPTURE); var_dump($matches); // approach 2 $string = '繁體中文-简体中文-English-12345-。,!-.,!-⭐'; $pattern = '/[\x{4e00}-\x{9fa5}]+/u'; preg_match_all($pattern, $string, $matches, PREG_OFFSET_CAPTURE); var_dump($matches); </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>。
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information