{"id":47800,"date":"2024-12-02T17:04:57","date_gmt":"2024-12-02T09:04:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47800\/"},"modified":"2024-12-02T17:04:57","modified_gmt":"2024-12-02T09:04:57","slug":"%e8%a7%a3%e9%94%81-php-%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e7%9a%84%e5%8f%8d%e5%90%91%e5%8c%b9%e9%85%8d%e5%8a%9b%e9%87%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47800\/","title":{"rendered":"\u89e3\u9501 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u53cd\u5411\u5339\u914d\u529b\u91cf"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u89e3\u9501 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u53cd\u5411\u5339\u914d\u529b\u91cf<\/h1>\n<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u89e3\u9501 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u53cd\u5411\u5339\u914d\u529b\u91cf\u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729936836671cbdc4831b3.jpg\" class=\"aligncenter\" title=\"\u89e3\u9501 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u53cd\u5411\u5339\u914d\u529b\u91cf\u63d2\u56fe\" alt=\"\u89e3\u9501 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u53cd\u5411\u5339\u914d\u529b\u91cf\u63d2\u56fe\" \/><\/p>\n<p><strong>\u89e3\u9501 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u53cd\u5411\u5339\u914d\u529b\u91cf<\/strong><\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u53cd\u5411\u5339\u914d\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6280\u672f\uff0c\u7528\u4e8e\u5339\u914d\u4e0d\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5b57\u7b26\u4e32\u3002\u901a\u8fc7\u4f7f\u7528 <code>^<\/code> \u548c <code>$<\/code> \u7b26\u53f7\u4f5c\u4e3a\u884c\u5b9a\u4f4d\u7b26\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u5339\u914d\u4ec5\u51fa\u73b0\u5728\u5b57\u7b26\u4e32\u7684\u5f00\u5934\u6216\u7ed3\u5c3e\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<p>\u53cd\u5411\u5339\u914d\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>preg_match('\/^(?!pattern)\/s', $subject);<\/pre>\n<ul>\n<li><strong>pattern\uff1a<\/strong>\u8981\u6392\u9664\u7684\u6a21\u5f0f\u3002<\/li>\n<li><strong>s\uff1a<\/strong>\u53ef\u9009\u7684 &#8220;\u591a\u884c&#8221; \u6807\u5fd7\uff0c\u5b83\u5141\u8bb8 ^ \u548c $ \u5339\u914d\u6bcf\u4e00\u884c\u7684\u5f00\u5934\u548c\u7ed3\u5c3e\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong>\u5339\u914d\u4e0d\u5305\u542b\u5b57\u6bcd &#8220;a&#8221; \u7684\u5b57\u7b26\u4e32<\/p>\n<pre>$pattern = '\/^(?!.*a).+$\/s';<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong>\u5339\u914d\u4ee5 &#8220;http:\/\/&#8221; \u4e0d\u5f00\u5934\u7684 URL<\/p>\n<pre>$pattern = '\/(?!http:\\\/\\\/)[-a-zA-Z0-9@:%_\\+.~#?&amp;\/=]{2,256}\\.[a-z]{2,4}\\b(\\\/[-a-zA-Z0-9@:%_\\+.~#?&amp;\/=]*)?\/';<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong>\u5339\u914d\u4e0d\u5305\u542b\u6570\u5b57\u7684\u59d3\u540d<\/p>\n<pre>$pattern = '\/\\b(?!.*\\d).+\\b\/';<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u53cd\u5411\u5339\u914d\u662f PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u4e00\u4e2a\u6709\u7528\u7684\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u60a8\u6392\u9664\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u7684\u5b57\u7b26\u4e32\u3002\u901a\u8fc7\u7ed3\u5408 <code>^<\/code> \u548c <code>$<\/code> \u7b26\u53f7\uff0c\u60a8\u53ef\u4ee5\u7cbe\u786e\u5730\u6307\u5b9a\u8981\u5728\u67e5\u627e\u4e2d\u6392\u9664\u7684\u6a21\u5f0f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u89e3\u9501 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u53cd\u5411\u5339\u914d\u529b\u91cf \u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9\u6587\u7ae0\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u6587\u7ae0\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587\u300a\u89e3\u9501 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u53cd\u5411\u5339\u914d\u529b\u91cf\u300b\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~ \u89e3\u9501 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u53cd\u5411\u5339\u914d\u529b\u91cf \u5728 PHP \u4e2d\uff0c\u53cd\u5411\u5339\u914d\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6280\u672f\uff0c\u7528\u4e8e\u5339\u914d\u4e0d\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5b57\u7b26\u4e32\u3002\u901a\u8fc7\u4f7f\u7528 ^ \u548c $ \u7b26\u53f7\u4f5c\u4e3a\u884c\u5b9a\u4f4d\u7b26\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u5339\u914d\u4ec5\u51fa\u73b0\u5728\u5b57\u7b26\u4e32\u7684\u5f00\u5934\u6216\u7ed3\u5c3e\u3002 \u8bed\u6cd5 \u53cd\u5411\u5339\u914d\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a preg_match(&#8216;\/^(?!pattern)\/s&#8217;, $subject); pattern\uff1a\u8981\u6392\u9664\u7684\u6a21\u5f0f\u3002 s\uff1a\u53ef\u9009\u7684 &#8220;\u591a\u884c&#8221; \u6807\u5fd7\uff0c\u5b83\u5141\u8bb8 ^ \u548c $ \u5339\u914d\u6bcf\u4e00\u884c\u7684\u5f00\u5934\u548c\u7ed3\u5c3e\u3002 \u5b9e\u6218\u6848\u4f8b \u793a\u4f8b 1\uff1a\u5339\u914d\u4e0d\u5305\u542b\u5b57\u6bcd &#8220;a&#8221; \u7684\u5b57\u7b26\u4e32 $pattern = &#8216;\/^(?!.*a).+$\/s&#8217;; \u793a\u4f8b 2\uff1a\u5339\u914d\u4ee5 &#8220;http:\/\/&#8221; \u4e0d\u5f00\u5934\u7684 URL $pattern = &#8216;\/(?!http:\\\/\\\/)[-a-zA-Z0-9@:%_\\+.~#?&amp;\/=]{2,256}\\.[a-z]{2,4}\\b(\\\/[-a-zA-Z0-9@:%_\\+.~#?&amp;\/=]*)?\/&#8217;; \u793a\u4f8b 3\uff1a\u5339\u914d\u4e0d\u5305\u542b\u6570\u5b57\u7684\u59d3\u540d $pattern = &#8216;\/\\b(?!.*\\d).+\\b\/&#8217;; \u7ed3\u8bba \u53cd\u5411\u5339\u914d\u662f PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u4e00\u4e2a\u6709\u7528\u7684\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u60a8\u6392\u9664\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u7684\u5b57\u7b26\u4e32\u3002\u901a\u8fc7\u7ed3\u5408 ^ \u548c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-47800","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47800","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=47800"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47800\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}