{"id":202414,"date":"2025-05-13T14:32:02","date_gmt":"2025-05-13T06:32:02","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202414\/"},"modified":"2025-05-13T14:32:02","modified_gmt":"2025-05-13T06:32:02","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8php%e4%b8%ad%e4%bd%bf%e7%94%a8%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e6%8f%90%e5%8f%96%e6%95%b0%e6%8d%ae%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202414\/","title":{"rendered":"\u5982\u4f55\u5728php\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6570\u636e\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u5728php\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6570\u636e\uff1f<\/h1>\n<p><span style=\"font-size: 15px\">\u6709\u5fd7\u8005\uff0c\u4e8b\u7adf\u6210\uff01\u5982\u679c\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5728php\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6570\u636e\uff1f\u300b<\/span>\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff01\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/span><\/p>\n<p>\u5728 PHP \u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6570\u636e\u7684\u65b9\u5f0f\u5305\u62ec\uff1a\u4f7f\u7528 preg_match() \u67e5\u627e\u5339\u914d\u9879\u5e76\u8fd4\u56de\u5e03\u5c14\u503c\u3002\u4f7f\u7528 preg_match_all() \u83b7\u53d6\u6240\u6709\u5339\u914d\u9879\u5e76\u8fd4\u56de\u5305\u542b\u5339\u914d\u9879\u7684\u6570\u7ec4\u3002\u4f7f\u7528 preg_replace() \u57fa\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5339\u914d\u9879\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241012\/1728738290670a73f2547aa.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u5728 PHP \u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6570\u636e\uff1f<\/strong><\/p>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u7528\u4e8e\u5339\u914d\u6587\u672c\u6a21\u5f0f\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u5728 PHP \u4e2d\u5e7f\u6cdb\u7528\u4e8e\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728 PHP \u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6570\u636e\uff1a<\/p>\n<p><strong>\u4f7f\u7528 preg_match()<\/strong><\/p>\n<p>preg_match() \u51fd\u6570\u7528\u4e8e\u57fa\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u5339\u914d\u9879\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u8868\u793a\u662f\u5426\u627e\u5230\u5339\u914d\u9879\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>$text = \"\u4f60\u7684\u540d\u5b57\u662f John Doe?\";\npreg_match(\"\/John Doe\/\", $text, $matches);\nif ($matches) {\n    echo \"\u59d3\u540d\uff1a\" . $matches[0];\n}<\/pre>\n<p><strong>\u4f7f\u7528 preg_match_all()<\/strong><\/p>\n<p>preg_match_all() \u51fd\u6570\u4e0e preg_match() \u7c7b\u4f3c\uff0c\u4f46\u5b83\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5339\u914d\u9879\u7684\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>$text = \"\u8fd9\u662f\u6211\u7684\u7535\u8bdd\u53f7\u7801\uff1a123-456-7890 \u548c 098-765-4321\";\npreg_match_all(\"\/\\d{3}-\\d{3}-\\d{4}\/\", $text, $matches);\nforeach ($matches[0] as $phone) {\n    echo \"\u7535\u8bdd\u53f7\u7801\uff1a\" . $phone . \"&lt;br&gt;\";\n}<\/pre>\n<p><strong>\u4f7f\u7528 preg_replace()<\/strong><\/p>\n<p>preg_replace() \u51fd\u6570\u7528\u4e8e\u57fa\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5339\u914d\u9879\u3002\u4f8b\u5982\uff0c\u8981\u4ece\u5b57\u7b26\u4e32\u4e2d\u5220\u9664\u6240\u6709\u6570\u5b57\uff1a<\/p>\n<pre>$text = \"\u53f7\u7801\u4e3a 123-456-7890\";\n$cleanText = preg_replace(\"\/\\d+\/\", \"\", $text);\necho $cleanText; \/\/ \u8f93\u51fa\uff1a\"\u53f7\u7801\u4e3a \"<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u8003\u8651\u4e00\u4e2a\u4ece HTML \u4e2d\u63d0\u53d6\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u7684\u6848\u4f8b\uff1a<\/p>\n<pre>function extractEmails($html) {\n    preg_match_all(\"\/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}\/\", $html, $matches);\n    return $matches[0];\n}\n\n$html = \"&lt;html&gt;&lt;body&gt;&lt;p&gt;\u7535\u5b50\u90ae\u4ef6\uff1ajohn.doe@example.com&lt;\/p&gt;&lt;\/body&gt;&lt;\/html&gt;\";\n$emails = extractEmails($html);\n\nforeach ($emails as $email) {\n    echo \"\u7535\u5b50\u90ae\u4ef6\uff1a\" . $email . \"&lt;br&gt;\";\n}<\/pre>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>\u7535\u5b50\u90ae\u4ef6\uff1ajohn.doe@example.com<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5982\u4f55\u5728php\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6570\u636e\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f,\u6570\u636e\u63d0\u53d6\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728php\u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6&#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-202414","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202414","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=202414"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202414\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}