{"id":202425,"date":"2025-05-13T13:08:30","date_gmt":"2025-05-13T05:08:30","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202425\/"},"modified":"2025-05-13T13:08:30","modified_gmt":"2025-05-13T05:08:30","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8php%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e8%bf%9b%e8%a1%8c%e5%ad%97%e7%ac%a6%e9%9b%86%e5%8c%b9%e9%85%8d%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202425\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528php\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5b57\u7b26\u96c6\u5339\u914d\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528php\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5b57\u7b26\u96c6\u5339\u914d\uff1f<\/h1>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300a\u5982\u4f55\u4f7f\u7528php\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5b57\u7b26\u96c6\u5339\u914d\uff1f\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p>PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5b57\u7b26\u96c6\u5339\u914d\u7b26\uff0c\u7528\u4e8e\u5339\u914d\u7279\u5b9a\u7684\u5b57\u7b26\u6a21\u5f0f\uff1a\u30101\u3011\u65b9\u62ec\u53f7 []: \u5339\u914d\u6307\u5b9a\u5b57\u7b26\u96c6\u5185\u7684\u4efb\u4f55\u5b57\u7b26\uff1b\u30102\u30111: \u5339\u914d\u6307\u5b9a\u5b57\u7b26\u96c6\u4e4b\u5916\u7684\u5b57\u7b26\uff1b\u30103\u3011\\d: \u5339\u914d\u6570\u5b57\u6216\u4e0e [0-9] \u7b49\u4ef7\uff1b\u30104\u3011\\D: \u5339\u914d\u975e\u6570\u5b57\u6216\u4e0e 2 \u7b49\u4ef7\uff1b\u30105\u3011\\w: \u5339\u914d\u5b57\u6bcd\u6570\u5b57\u6216\u4e0e [a-zA-Z0-9_] \u7b49\u4ef7\uff1b\u30106\u3011\\W: \u5339\u914d\u975e\u5b57\u6bcd\u6570\u5b57\u6216\u4e0e 3 \u7b49\u4ef7\u3002&#8230; \u21a90-9 \u21a9a-zA-Z0-9_ \u21a9<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241013\/1728817309670ba89d39445.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>PHP \u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u5b57\u7b26\u96c6\u5339\u914d<\/strong><\/p>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u7528\u4e8e\u5339\u914d\u548c\u5904\u7406\u6587\u672c\u7684\u4e00\u79cd\u5f3a\u5927\u5de5\u5177\u3002PHP \u63d0\u4f9b\u4e86\u4e00\u7ec4\u5b57\u7b26\u96c6\u5339\u914d\u7b26\uff0c\u5141\u8bb8\u60a8\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7b26\u5408\u7279\u5b9a\u6a21\u5f0f\u7684\u5b57\u7b26\u3002<\/p>\n<p><strong>\u5b57\u7b26\u96c6\u5339\u914d\u7b26<\/strong><\/p>\n<ul>\n<li><code>[]<\/code>: \u5339\u914d\u65b9\u62ec\u53f7\u5185\u6307\u5b9a\u7684\u5b57\u7b26\u96c6\u4e2d\u7684\u4efb\u4f55\u4e00\u4e2a\u5b57\u7b26\u3002<\/li>\n<li><code>[^...]<\/code>: \u5339\u914d\u65b9\u62ec\u53f7\u5185\u6307\u5b9a\u7684\u5b57\u7b26\u96c6\u4e4b\u5916\u7684\u4efb\u4f55\u5b57\u7b26\u3002<\/li>\n<li><code>\\d<\/code>: \u5339\u914d\u4efb\u4f55\u6570\u5b57\u3002\u7b49\u540c\u4e8e [0-9]\u3002<\/li>\n<li><code>\\D<\/code>: \u5339\u914d\u4efb\u4f55\u975e\u6570\u5b57\u3002\u7b49\u540c\u4e8e <sup><\/sup>\u3002<\/li>\n<li><code>\\w<\/code>: \u5339\u914d\u4efb\u4f55\u5b57\u6bcd\u6570\u5b57\u5b57\u7b26\u3002\u7b49\u540c\u4e8e [a-zA-Z0-9_]\u3002<\/li>\n<li><code>\\W<\/code>: \u5339\u914d\u4efb\u4f55\u975e\u5b57\u6bcd\u6570\u5b57\u5b57\u7b26\u3002\u7b49\u540c\u4e8e <sup><\/sup>\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>1. \u5339\u914d\u6570\u5b57\uff08\\d\uff09<\/strong><\/p>\n<pre>$pattern = '\/\\d\/';\n$subject = '12345';\n$matches = preg_match_all($pattern, $subject, $matches);\nprint_r($matches);<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>Array\n(\n    [0] =&gt; Array\n        (\n            [0] =&gt; 1\n            [1] =&gt; 2\n            [2] =&gt; 3\n            [3] =&gt; 4\n            [4] =&gt; 5\n        )\n\n)<\/pre>\n<p><strong>2. \u5339\u914d\u975e\u6570\u5b57\uff08\\D\uff09<\/strong><\/p>\n<pre>$pattern = '\/\\D\/';\n$subject = 'Test123';\n$matches = preg_match_all($pattern, $subject, $matches);\nprint_r($matches);<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>Array\n(\n    [0] =&gt; Array\n        (\n            [0] =&gt; T\n            [1] =&gt; e\n            [2] =&gt; s\n            [3] =&gt; t\n        )\n)<\/pre>\n<p><strong>3. \u5339\u914d\u5b57\u6bcd\u6570\u5b57\uff08\\w\uff09<\/strong><\/p>\n<pre>$pattern = '\/\\w\/';\n$subject = 'My_Username123';\n$matches = preg_match_all($pattern, $subject, $matches);\nprint_r($matches);<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>Array\n(\n    [0] =&gt; Array\n        (\n            [0] =&gt; M\n            [1] =&gt; y\n            [2] =&gt; _\n            [3] =&gt; U\n            [4] =&gt; s\n            [5] =&gt; e\n            [6] =&gt; r\n            [7] =&gt; n\n            [8] =&gt; a\n            [9] =&gt; m\n            [10] =&gt; e\n            [11] =&gt; 1\n            [12] =&gt; 2\n            [13] =&gt; 3\n        )\n)<\/pre>\n<p><strong>4. \u5339\u914d\u975e\u5b57\u6bcd\u6570\u5b57\uff08\\W\uff09<\/strong><\/p>\n<pre>$pattern = '\/\\W\/';\n$subject = 'My Username!@#$%^&amp;*()';\n$matches = preg_match_all($pattern, $subject, $matches);\nprint_r($matches);<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>Array\n(\n    [0] =&gt; Array\n        (\n            [0] =&gt; !\n            [1] =&gt; @\n            [2] =&gt; #\n            [3] =&gt; $\n            [4] =&gt; %\n            [5] =&gt; ^\n            [6] =&gt; &amp;\n            [7] =&gt; *\n            [8] =&gt; (\n            [9] =&gt; )\n            [10] =&gt;  \n        )\n)<\/pre>\n<hr>\n<ol>\n<li>0-9 <\/li>\n<li>a-zA-Z0-9_ <\/li>\n<\/ol>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528php\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5b57\u7b26&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202425","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202425"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202425\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}