Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
LemonWiki共筆
Search
Search
Appearance
Log in
Personal tools
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Regular expression in Mandarin
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
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!
==== 尋找 ASCII 字元 in PHP ==== Code I: <pre> if (preg_match('/[^\x20-\x7f]/', $keyword) === 0) { echo "The keyword is ASCII only"; } else { echo "The keyword contains non-ASCII characters (like Chinese, Japanese, etc.)"; } </pre> Explanation: * {{kbd | key=<nowiki>/[^\x20-\x7f]/</nowiki>}} 是一個正規表達式: ** {{kbd | key=<nowiki>\x20</nowiki>}} 代表空格字符 (ASCII 32) ** {{kbd | key=<nowiki>\x7f</nowiki>}} 代表 DELETE 字符 (ASCII 127) ** {{kbd | key=<nowiki>[^\x20-\x7f]</nowiki>}} 表示「任何不在 \x20 到 \x7f 範圍內的字符」 這個範圍 {{kbd | key=<nowiki>(\x20-\x7f)</nowiki>}} 基本上涵蓋了所有可印刷的 ASCII 字符;不符合條件,則表示字串中包含非 ASCII 字符 (如中文、日文等) Code II<ref>[https://www.php.net/manual/en/regexp.reference.character-classes.php PHP: Character classes - Manual]</ref> <pre> $pattern = '/^[[:ascii:]]+$/i'; $text = "Hello World"; // ASCII only if (preg_match($pattern, $text)) { echo "純 ASCII 字元"; } else { echo "包含非 ASCII 字元"; } $text = "Hello 世界"; // Contains non-ASCII if (preg_match($pattern, $text)) { echo "純 ASCII 字元"; } else { echo "包含非 ASCII 字元"; } </pre> Explanation: * 匹配整個字串({{kbd | key=<nowiki>^</nowiki>}} 和 {{kbd | key=<nowiki>$</nowiki>}}) * 需要符合一個或多個 ASCII 字元({{kbd | key=<nowiki>[[:ascii:]]+</nowiki>}}) * 不區分大小寫({{kbd | key=<nowiki>i</nowiki>}} flag)
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)