{"id":202444,"date":"2025-05-13T10:38:57","date_gmt":"2025-05-13T02:38:57","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202444\/"},"modified":"2025-05-13T10:38:57","modified_gmt":"2025-05-13T02:38:57","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%e6%96%87%e6%9c%ac%e5%88%86%e6%9e%90%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202444\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528php\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6587\u672c\u5206\u6790\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528php\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6587\u672c\u5206\u6790\uff1f<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528php\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6587\u672c\u5206\u6790\uff1f\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p>PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u7528\u4e8e\u5728\u6587\u672c\u4e2d\u641c\u7d22\u7279\u5b9a\u6a21\u5f0f\uff0c\u5e38\u7528\u51fd\u6570\u5305\u62ec preg_match() \u548c preg_replace()\u3002\u8bed\u6cd5\u5305\u62ec\uff1a.\uff08\u5339\u914d\u4efb\u4f55\u5b57\u7b26\uff09\u3001\\d\uff08\u5339\u914d\u6570\u5b57\uff09\u3001[]\uff08\u5b57\u7b26\u96c6\uff09\u3001[^]\uff08\u6392\u9664\u5b57\u7b26\u96c6\uff09\uff0c{}\uff08\u6307\u5b9a\u5339\u914d\u6b21\u6570\uff09\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u67e5\u627e\u4ee5 &#8220;ing&#8221; \u7ed3\u5c3e\u7684\u5355\u8bcd\u3001\u5339\u914d\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3001\u4ece HTML \u63d0\u53d6\u56fe\u50cf URL \u548c\u9a8c\u8bc1\u5bc6\u7801\u5f3a\u5ea6\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241017\/17291564906710d58a0d1cf.jpg\" class=\"aligncenter\"><\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6587\u672c\u5206\u6790<\/h2>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6a21\u5f0f\u5339\u914d\u5de5\u5177\uff0c\u53ef\u7528\u4e8e\u5728\u6587\u672c\u4e2d\u641c\u7d22\u548c\u67e5\u627e\u7279\u5b9a\u7684\u6a21\u5f0f\u3002\u5b66\u4e60\u5982\u4f55\u4f7f\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u6587\u672c\u5904\u7406\u4efb\u52a1\u7684\u6548\u7387\u3002<\/p>\n<h3>\u57fa\u7840\u8bed\u6cd5<\/h3>\n<p>PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u4f7f\u7528 <code>preg_match()<\/code>, <code>preg_match_all()<\/code>, <code>preg_replace()<\/code> \u7b49\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684\u5e38\u7528\u8bed\u6cd5\uff1a<\/p>\n<ul>\n<li><strong><code>.<\/code>\uff1a\u5339\u914d\u4efb\u4f55\u4e00\u4e2a\u5b57\u7b26<\/strong><\/li>\n<li><strong>\\d\uff1a\u5339\u914d\u4efb\u4f55\u6570\u5b57<\/strong><\/li>\n<li><strong>\\w\uff1a\u5339\u914d\u4efb\u4f55\u5b57\u6bcd\u6570\u5b57\u5b57\u7b26<\/strong><\/li>\n<li><strong>\\s\uff1a\u5339\u914d\u4efb\u4f55\u7a7a\u767d\u5b57\u7b26<\/strong><\/li>\n<li><strong>[]\uff1a\u5339\u914d\u6307\u5b9a\u5b57\u7b26\u96c6\u4e2d\u7684\u5b57\u7b26<\/strong><\/li>\n<li><strong>[^]\uff1a\u5339\u914d\u4e0d\u5c5e\u4e8e\u6307\u5b9a\u5b57\u7b26\u96c6\u4e2d\u7684\u5b57\u7b26<\/strong><\/li>\n<li><strong>{}\uff1a\u6307\u5b9a\u5339\u914d\u6b21\u6570<\/strong><\/li>\n<\/ul>\n<h3>\u5b9e\u6218\u6848\u4f8b<\/h3>\n<p><strong>\u67e5\u627e\u6240\u6709\u4ee5 &#8220;ing&#8221; \u7ed3\u5c3e\u7684\u5355\u8bcd\uff1a<\/strong><\/p>\n<pre>$text = \"The boy is eating a sandwich.\";\npreg_match_all('\/(.*ing)\/', $text, $matches);\nprint_r($matches);<\/pre>\n<p><strong>\u5339\u914d\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff1a<\/strong><\/p>\n<pre>$email = \"john.doe@example.com\";\npreg_match('\/^[\\w\\-\\.]+@[\\w\\-\\.]+\\.\\w+$\/', $email, $matches);\nif (count($matches) &gt; 0) {\n  echo 'Valid email address';\n}<\/pre>\n<p><strong>\u4ece HTML \u4e2d\u63d0\u53d6\u56fe\u50cf URL\uff1a<\/strong><\/p>\n<pre>$html = \"&lt;img src=\\\"image.jpg\\\" alt=\\\"\\\" \/&gt;\";\npreg_match('\/&lt;img.*?src=\"(.*?)\"\/', $html, $matches);\necho $matches[1]; \/\/ \u8f93\u51fa\uff1a\"image.jpg\"<\/pre>\n<p><strong>\u9a8c\u8bc1\u5bc6\u7801\u5f3a\u5ea6\uff1a<\/strong><\/p>\n<pre>$password = \"password123\";\npreg_match('\/^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[\\W_]).+$\/', $password, $matches);\nif (count($matches) &gt; 0) {\n  echo 'Strong password';\n}<\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u5b9e\u6218\u6848\u4f8b\uff0c\u4f60\u53ef\u4ee5\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u6267\u884c\u5404\u79cd\u6587\u672c\u5904\u7406\u4efb\u52a1\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528php\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6587\u672c\u5206\u6790\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528php\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6587\u672c&#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-202444","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202444","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=202444"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202444\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}