{"id":202475,"date":"2025-05-13T10:29:11","date_gmt":"2025-05-13T02:29:11","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202475\/"},"modified":"2025-05-13T10:29:11","modified_gmt":"2025-05-13T02:29:11","slug":"php-%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e5%9c%a8%e7%88%ac%e8%99%ab%e4%b8%ad%e7%9a%84%e7%a5%9e%e5%a5%87%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202475\/","title":{"rendered":"PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u722c\u866b\u4e2d\u7684\u795e\u5947\u5e94\u7528"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u722c\u866b\u4e2d\u7684\u795e\u5947\u5e94\u7528<\/h1>\n<p><span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898<span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u662f<\/span><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u722c\u866b\u4e2d\u7684\u795e\u5947\u5e94\u7528\u300b<\/span>\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01<\/span><\/p>\n<p>PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u722c\u866b\u5e94\u7528\uff1a\u63d0\u4f9b\u9ad8\u6548\u7684\u6587\u672c\u6570\u636e\u5339\u914d\u548c\u89e3\u6790\u80fd\u529b\u3002\u4f7f\u7528 preg_match()\u3001preg_match_all() \u548c preg_replace() \u51fd\u6570\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u8bed\u6cd5\u5305\u62ec\u7279\u6b8a\u5b57\u7b26\u548c\u5143\u5b57\u7b26\uff0c\u7528\u4e8e\u5339\u914d\u7279\u5b9a\u6a21\u5f0f\u3002\u5b9e\u9645\u5e94\u7528\u793a\u4f8b\u5305\u62ec\u63d0\u53d6\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff08[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,6}\uff09\u548c URL\uff08(https?:\/\/1+)\uff09\u3002\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4ece HTML \u4ee3\u7801\u4e2d\u63d0\u53d6\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u548c URL\u3002\\s \u21a9<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729837247671b38bf6f0f4.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u722c\u866b\u4e2d\u7684\u795e\u5947\u5e94\u7528<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6a21\u5f0f\u5339\u914d\u5de5\u5177\uff0c\u5728\u722c\u866b\u4e2d\u53ef\u4ee5\u7528\u6765\u4ece HTML \u6216\u5176\u4ed6\u6587\u672c\u6570\u636e\u4e2d\u63d0\u53d6\u7279\u5b9a\u4fe1\u606f\u3002\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u4e14\u51c6\u786e\u5730\u67e5\u627e\u548c\u89e3\u6790\u6570\u636e\u7684\u80fd\u529b\u3002<\/p>\n<p><strong>\u4f7f\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>preg_match()<\/code>\u3001<code>preg_match_all()<\/code> \u548c <code>preg_replace()<\/code> \u7b49\u51fd\u6570\u6765\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u8fd9\u4e9b\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u4e3b\u8981\u53c2\u6570\uff1a\u8981\u5339\u914d\u7684\u6a21\u5f0f\u548c\u8981\u641c\u7d22\u7684\u6587\u672c\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u8bed\u6cd5\u7531\u5404\u79cd\u7279\u6b8a\u5b57\u7b26\u548c\u5143\u5b57\u7b26\u7ec4\u6210\uff0c\u7528\u4e8e\u5339\u914d\u6587\u672c\u4e2d\u7684\u7279\u5b9a\u6a21\u5f0f\uff1a<\/p>\n<ul>\n<li><code>.<\/code>\uff1a\u5339\u914d\u4efb\u4f55\u5355\u4e2a\u5b57\u7b26<\/li>\n<li><code>*<\/code>\uff1a\u5339\u914d\u524d\u4e00\u4e2a\u5b57\u7b26 0 \u6b21\u6216\u591a\u6b21<\/li>\n<li><code>+<\/code>\uff1a\u5339\u914d\u524d\u4e00\u4e2a\u5b57\u7b26 1 \u6b21\u6216\u591a\u6b21<\/li>\n<li><code>?<\/code>\uff1a\u5339\u914d\u524d\u4e00\u4e2a\u5b57\u7b26 0 \u6b21\u6216 1 \u6b21<\/li>\n<li><code>^<\/code>\uff1a\u5339\u914d\u5b57\u7b26\u4e32\u7684\u5f00\u59cb<\/li>\n<li><code>$<\/code>\uff1a\u5339\u914d\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e<\/li>\n<li><code>[]<\/code>\uff1a\u8868\u793a\u5b57\u7b26\u7c7b\uff0c\u5339\u914d\u5176\u4e2d\u7684\u4efb\u4f55\u4e00\u4e2a\u5b57\u7b26<\/li>\n<li><code>()<\/code>: \u521b\u5efa\u5b50\u6a21\u5f0f\uff0c\u6355\u83b7\u4e0e\u6a21\u5f0f\u5339\u914d\u7684\u6587\u672c<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u63d0\u53d6\u7535\u5b50\u90ae\u4ef6\u5730\u5740<\/strong><\/p>\n<p>\u4ee5\u4e0b\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u7528\u4e8e\u4ece HTML \u4ee3\u7801\u4e2d\u63d0\u53d6\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff1a<\/p>\n<pre>$pattern = \"\/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}\/\";<\/pre>\n<p><strong>\u63d0\u53d6 URL<\/strong><\/p>\n<p>\u4ee5\u4e0b\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u7528\u4e8e\u4ece\u6587\u672c\u4e2d\u63d0\u53d6 URL\uff1a<\/p>\n<pre>$pattern = \"\/(https?:\\\/\\\/[^\\s]+)\/\";<\/pre>\n<p><strong>\u5b9e\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4ece HTML \u4ee3\u7801\u4e2d\u63d0\u53d6\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u548c URL\uff1a<\/p>\n<pre>$html = \"&lt;html&gt;\n    &lt;body&gt;\n        &lt;a href='https:\/\/www.example.com'&gt;Example&lt;\/a&gt;\n        &lt;span&gt;Email: user@example.com&lt;\/span&gt;\n    &lt;\/body&gt;\n&lt;\/html&gt;\";\n\n$email_matches = [];\npreg_match_all('\/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}\/', $html, $email_matches);\n\n$url_matches = [];\npreg_match_all('\/(https?:\\\/\\\/[^\\s]+)\/', $html, $url_matches);\n\nprint_r($email_matches[0]);\nprint_r($url_matches[0]);<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>Array\n(\n    [0] =&gt; user@example.com\n)\nArray\n(\n    [0] =&gt; https:\/\/www.example.com\n)<\/pre>\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>PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u722c\u866b\u4e2d\u7684\u795e\u5947&#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-202475","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202475","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=202475"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202475\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}