{"id":47671,"date":"2024-12-02T17:42:50","date_gmt":"2024-12-02T09:42:50","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47671\/"},"modified":"2024-12-02T17:42:50","modified_gmt":"2024-12-02T09:42:50","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%e5%ae%9e%e7%8e%b0%e6%9f%a5%e6%89%be%e5%92%8c%e6%9b%bf%e6%8d%a2%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47671\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528php\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9e\u73b0\u67e5\u627e\u548c\u66ff\u6362\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528php\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9e\u73b0\u67e5\u627e\u548c\u66ff\u6362\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u5982\u4f55\u4f7f\u7528php\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9e\u73b0\u67e5\u627e\u548c\u66ff\u6362\uff1f\u300b\uff0c\u8986\u76d6\u4e86\u6587\u7ae0\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p>\u4f7f\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u5b9e\u73b0\u67e5\u627e\u548c\u66ff\u6362\uff1a\u67e5\u627e\uff1a \u4f7f\u7528 preg_match() \u51fd\u6570\uff0c\u4f20\u5165\u6a21\u5f0f\u548c\u5b57\u7b26\u4e32\uff0c\u5339\u914d\u9879\u5c06\u5b58\u50a8\u5728\u6570\u7ec4\u4e2d\u3002\u66ff\u6362\uff1a \u4f7f\u7528 preg_replace() \u51fd\u6570\uff0c\u4f20\u5165\u6a21\u5f0f\u3001\u66ff\u6362\u5b57\u7b26\u4e32\u548c\u76ee\u6807\u5b57\u7b26\u4e32\uff0c\u6267\u884c\u66ff\u6362\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729929405671ca0bd8f4f5.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528php\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9e\u73b0\u67e5\u627e\u548c\u66ff\u6362\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528php\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9e\u73b0\u67e5\u627e\u548c\u66ff\u6362\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u5b9e\u73b0\u67e5\u627e\u548c\u66ff\u6362<\/strong><\/p>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6a21\u5f0f\u5339\u914d\u5de5\u5177\uff0c\u53ef\u7528\u4e8e\u6267\u884c\u9ad8\u7ea7\u6587\u672c\u641c\u7d22\u548c\u66ff\u6362\u64cd\u4f5c\u3002PHP \u5185\u7f6e\u4e86\u5f3a\u5927\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5f15\u64ce\uff0c\u672c\u6587\u5c06\u6f14\u793a\u5982\u4f55\u4f7f\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u6267\u884c\u67e5\u627e\u548c\u66ff\u6362\u3002<\/p>\n<p><strong>\u67e5\u627e<\/strong><\/p>\n<p>\u8981\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u7684\u6a21\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>preg_match()<\/code> \u51fd\u6570\u3002\u8be5\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>preg_match(pattern, subject, matches);<\/pre>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li><code>pattern<\/code> \u662f\u8981\u67e5\u627e\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/li>\n<li><code>subject<\/code> \u662f\u8981\u641c\u7d22\u7684\u5b57\u7b26\u4e32<\/li>\n<li><code>matches<\/code> \u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u7528\u4e8e\u5b58\u50a8\u5339\u914d\u9879<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a\u67e5\u627e\u6570\u5b57<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6570\u5b57\uff1a<\/p>\n<pre>$subject = \"My phone number is 123-456-7890.\";\n$pattern = \"\/\\d+\/\";\npreg_match($pattern, $subject, $matches);\nvar_dump($matches);<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>array(1) {\n  [0]=&gt;\n  string(10) \"123-456-7890\"\n}<\/pre>\n<p><strong>\u66ff\u6362<\/strong><\/p>\n<p>\u8981\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u6a21\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>preg_replace()<\/code> \u51fd\u6570\u3002\u8be5\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>preg_replace(pattern, replacement, subject);<\/pre>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li><code>pattern<\/code> \u662f\u8981\u67e5\u627e\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<\/li>\n<li><code>replacement<\/code> \u662f\u8981\u4f7f\u7528\u7684\u66ff\u6362\u5b57\u7b26\u4e32<\/li>\n<li><code>subject<\/code> \u662f\u8981\u6267\u884c\u66ff\u6362\u7684\u5b57\u7b26\u4e32<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a\u66ff\u6362 email \u5730\u5740<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709 email \u5730\u5740\uff1a<\/p>\n<pre>$subject = \"My email address is john@example.com.\";\n$pattern = \"\/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}\/\";\n$replacement = \"username@example.com\";\n$newSubject = preg_replace($pattern, $replacement, $subject);\necho $newSubject;<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>My email address is username@example.com.<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u6b63\u6587\u8fc7\u6ee4<\/strong><\/p>\n<p>\u6b64 PHP \u7a0b\u5e8f\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fc7\u6ee4\u6b63\u6587\u4e2d\u7684 HTML \u6807\u8bb0\uff1a<\/p>\n<pre>$comment = \"This comment contains &lt;strong&gt;strong&lt;\/strong&gt; tags.\";\n$pattern = \"\/(&lt;\\\/?strong&gt;)\/\";\n$filteredComment = preg_replace($pattern, \"\", $comment);\necho $filteredComment;<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>This comment contains strong tags.<\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u793a\u4f8b\uff0c\u60a8\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u4f7f\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u6267\u884c\u5f3a\u5927\u7684\u6587\u672c\u64cd\u4f5c\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>\u5982\u4f55\u4f7f\u7528php\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9e\u73b0\u67e5\u627e\u548c\u66ff\u6362\uff1f \u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u5982\u4f55\u4f7f\u7528php\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9e\u73b0\u67e5\u627e\u548c\u66ff\u6362\uff1f\u300b\uff0c\u8986\u76d6\u4e86\u6587\u7ae0\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002 \u4f7f\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u5b9e\u73b0\u67e5\u627e\u548c\u66ff\u6362\uff1a\u67e5\u627e\uff1a \u4f7f\u7528 preg_match() \u51fd\u6570\uff0c\u4f20\u5165\u6a21\u5f0f\u548c\u5b57\u7b26\u4e32\uff0c\u5339\u914d\u9879\u5c06\u5b58\u50a8\u5728\u6570\u7ec4\u4e2d\u3002\u66ff\u6362\uff1a \u4f7f\u7528 preg_replace() \u51fd\u6570\uff0c\u4f20\u5165\u6a21\u5f0f\u3001\u66ff\u6362\u5b57\u7b26\u4e32\u548c\u76ee\u6807\u5b57\u7b26\u4e32\uff0c\u6267\u884c\u66ff\u6362\u3002 \u5982\u4f55\u4f7f\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u5b9e\u73b0\u67e5\u627e\u548c\u66ff\u6362 \u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6a21\u5f0f\u5339\u914d\u5de5\u5177\uff0c\u53ef\u7528\u4e8e\u6267\u884c\u9ad8\u7ea7\u6587\u672c\u641c\u7d22\u548c\u66ff\u6362\u64cd\u4f5c\u3002PHP \u5185\u7f6e\u4e86\u5f3a\u5927\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5f15\u64ce\uff0c\u672c\u6587\u5c06\u6f14\u793a\u5982\u4f55\u4f7f\u7528 PHP \u6b63\u5219\u8868\u8fbe\u5f0f\u6267\u884c\u67e5\u627e\u548c\u66ff\u6362\u3002 \u67e5\u627e \u8981\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u7684\u6a21\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528 preg_match() \u51fd\u6570\u3002\u8be5\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a preg_match(pattern, subject, matches); \u5176\u4e2d\uff1a pattern \u662f\u8981\u67e5\u627e\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f subject \u662f\u8981\u641c\u7d22\u7684\u5b57\u7b26\u4e32 matches \u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u7528\u4e8e\u5b58\u50a8\u5339\u914d\u9879 \u793a\u4f8b\uff1a\u67e5\u627e\u6570\u5b57 \u4ee5\u4e0b\u4ee3\u7801\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6570\u5b57\uff1a $subject = &#8220;My phone number is 123-456-7890.&#8221;; $pattern = &#8220;\/\\d+\/&#8221;; preg_match($pattern, $subject, $matches); var_dump($matches); \u8f93\u51fa\uff1a array(1) { [0]=&gt; string(10) &#8220;123-456-7890&#8221; [&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-47671","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47671","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=47671"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47671\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}