{"id":46730,"date":"2024-12-02T15:43:07","date_gmt":"2024-12-02T07:43:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46730\/"},"modified":"2024-12-02T15:43:07","modified_gmt":"2024-12-02T07:43:07","slug":"php-%e9%80%92%e5%bd%92%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e5%af%b9-html-%e6%96%87%e6%a1%a3%e8%bf%9b%e8%a1%8c%e9%81%8d%e5%8e%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46730\/","title":{"rendered":"PHP \u9012\u5f52\u51fd\u6570\u5982\u4f55\u5bf9 HTML \u6587\u6863\u8fdb\u884c\u904d\u5386"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u9012\u5f52\u51fd\u6570\u5982\u4f55\u5bf9 HTML \u6587\u6863\u8fdb\u884c\u904d\u5386<\/h1>\n<p>\u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aPHP \u9012\u5f52\u51fd\u6570\u5982\u4f55\u5bf9 HTML \u6587\u6863\u8fdb\u884c\u904d\u5386\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p>\u9012\u5f52\u51fd\u6570\u5728 PHP \u4e2d\u53ef\u7528\u4e8e\u904d\u5386\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982 HTML \u6587\u6863\u3002\u5b83\u672c\u8d28\u4e0a\u662f\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u7684\u65b9\u6cd5\u3002\u8be5\u6280\u672f\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\u5177\u6709\u4f18\u52bf\u3002\u4e00\u4e2a\u57fa\u672c\u7528\u6cd5\u662f\u904d\u5386 HTML \u6587\u6863\uff0c\u6253\u5370\u8282\u70b9\u540d\u79f0\u5e76\u9012\u5f52\u8c03\u7528\u5b50\u8282\u70b9\u3002\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u662f\u63d0\u53d6 HTML \u6587\u6863\u4e2d\u7684\u6587\u672c\uff0c\u5176\u4e2d\u51fd\u6570\u9644\u52a0\u5f53\u524d\u8282\u70b9\u7684\u6587\u672c\u503c\u5e76\u9012\u5f52\u904d\u5386\u5b50\u8282\u70b9\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729842748671b4e3c6c4cc.jpg\" class=\"aligncenter\" title=\"PHP \u9012\u5f52\u51fd\u6570\u5982\u4f55\u5bf9 HTML \u6587\u6863\u8fdb\u884c\u904d\u5386\u63d2\u56fe\" alt=\"PHP \u9012\u5f52\u51fd\u6570\u5982\u4f55\u5bf9 HTML \u6587\u6863\u8fdb\u884c\u904d\u5386\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u9012\u5f52\u51fd\u6570\u904d\u5386 HTML \u6587\u6863<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u9012\u5f52\u51fd\u6570\u662f\u4e00\u79cd\u5728 PHP \u4e2d\u7528\u4e8e\u904d\u5386\u6570\u636e\u7ed3\u6784\u7684\u5f3a\u5927\u6280\u672f\uff0c\u672c\u8d28\u4e0a\u662f\u5f53\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u65f6\u53d1\u751f\u7684\u60c5\u51b5\u3002\u5728\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff08\u4f8b\u5982 HTML \u6587\u6863\uff09\u65f6\uff0c\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u5177\u6709\u5f88\u5927\u7684\u4f18\u52bf\u3002<\/p>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u904d\u5386 HTML \u6587\u6863\u7684\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<pre>function traverse_html($html) {\n  \/\/ \u5982\u679c\u5f53\u524d\u8282\u70b9\u662f\u5143\u7d20\uff0c\u5219\u6253\u5370\u5176\u540d\u79f0\n  if ($html-&gt;nodeName) {\n    echo $html-&gt;nodeName . \"&lt;br&gt;\";\n  }\n\n  \/\/ \u904d\u5386\u5b50\u8282\u70b9\n  foreach ($html-&gt;childNodes as $child) {\n    \/\/ \u9012\u5f52\u8c03\u7528\u51fd\u6570\u904d\u5386\u5b50\u8282\u70b9\n    traverse_html($child);\n  }\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u63d0\u53d6\u6587\u672c<\/strong><\/p>\n<p>\u8981\u6f14\u793a\u9012\u5f52\u51fd\u6570\u7684\u5b9e\u9645\u5e94\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u4ece HTML \u6587\u6863\u4e2d\u63d0\u53d6\u7eaf\u6587\u672c\uff1a<\/p>\n<pre>function extract_text($html) {\n  $text = \"\";\n\n  \/\/ \u5982\u679c\u5f53\u524d\u8282\u70b9\u662f\u6587\u672c\uff0c\u5219\u9644\u52a0\u5230\u7ed3\u679c\n  if ($html-&gt;nodeType == XML_TEXT_NODE) {\n    $text .= $html-&gt;nodeValue;\n  }\n\n  \/\/ \u904d\u5386\u5b50\u8282\u70b9\u5e76\u9012\u5f52\u8c03\u7528\u51fd\u6570\n  foreach ($html-&gt;childNodes as $child) {\n    $text .= extract_text($child);\n  }\n\n  return $text;\n}<\/pre>\n<p><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/p>\n<p>\u8981\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u52a0\u8f7d HTML \u6587\u6863\u5230 DOMDocument \u5bf9\u8c61\u5e76\u8c03\u7528\u904d\u5386\u51fd\u6570\uff1a<\/p>\n<pre>$html_doc = new DOMDocument();\n$html_doc-&gt;loadHTMLFile('document.html');\n\n\/\/ \u904d\u5386\u548c\u6253\u5370 HTML \u6587\u6863\u7684\u8282\u70b9\u540d\u79f0\ntraverse_html($html_doc);\n\n\/\/ \u63d0\u53d6 HTML \u6587\u6863\u4e2d\u7684\u7eaf\u6587\u672c\n$text = extract_text($html_doc);\necho $text;<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aPHP \u9012\u5f52\u51fd\u6570\u5982\u4f55\u5bf9 HTML \u6587\u6863\u8fdb\u884c\u904d\u5386\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8ephp,\u9012\u5f52\u51fd\u6570\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u9012\u5f52\u51fd\u6570\u5982\u4f55\u5bf9 HTML \u6587\u6863\u8fdb\u884c\u904d\u5386 \u6587\u7ae0\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aPHP \u9012\u5f52\u51fd\u6570\u5982\u4f55\u5bf9 HTML \u6587\u6863\u8fdb\u884c\u904d\u5386\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 \u9012\u5f52\u51fd\u6570\u5728 PHP \u4e2d\u53ef\u7528\u4e8e\u904d\u5386\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982 HTML \u6587\u6863\u3002\u5b83\u672c\u8d28\u4e0a\u662f\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u7684\u65b9\u6cd5\u3002\u8be5\u6280\u672f\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\u5177\u6709\u4f18\u52bf\u3002\u4e00\u4e2a\u57fa\u672c\u7528\u6cd5\u662f\u904d\u5386 HTML \u6587\u6863\uff0c\u6253\u5370\u8282\u70b9\u540d\u79f0\u5e76\u9012\u5f52\u8c03\u7528\u5b50\u8282\u70b9\u3002\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u662f\u63d0\u53d6 HTML \u6587\u6863\u4e2d\u7684\u6587\u672c\uff0c\u5176\u4e2d\u51fd\u6570\u9644\u52a0\u5f53\u524d\u8282\u70b9\u7684\u6587\u672c\u503c\u5e76\u9012\u5f52\u904d\u5386\u5b50\u8282\u70b9\u3002 PHP \u9012\u5f52\u51fd\u6570\u904d\u5386 HTML \u6587\u6863 \u7b80\u4ecb \u9012\u5f52\u51fd\u6570\u662f\u4e00\u79cd\u5728 PHP \u4e2d\u7528\u4e8e\u904d\u5386\u6570\u636e\u7ed3\u6784\u7684\u5f3a\u5927\u6280\u672f\uff0c\u672c\u8d28\u4e0a\u662f\u5f53\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u65f6\u53d1\u751f\u7684\u60c5\u51b5\u3002\u5728\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff08\u4f8b\u5982 HTML \u6587\u6863\uff09\u65f6\uff0c\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u5177\u6709\u5f88\u5927\u7684\u4f18\u52bf\u3002 \u57fa\u672c\u7528\u6cd5 \u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u904d\u5386 HTML \u6587\u6863\u7684\u57fa\u672c\u793a\u4f8b\uff1a function traverse_html($html) { \/\/ \u5982\u679c\u5f53\u524d\u8282\u70b9\u662f\u5143\u7d20\uff0c\u5219\u6253\u5370\u5176\u540d\u79f0 if ($html-&gt;nodeName) { echo $html-&gt;nodeName . &#8220;&lt;br&gt;&#8221;; } \/\/ \u904d\u5386\u5b50\u8282\u70b9 foreach ($html-&gt;childNodes as $child) { \/\/ \u9012\u5f52\u8c03\u7528\u51fd\u6570\u904d\u5386\u5b50\u8282\u70b9 traverse_html($child); } [&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-46730","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46730","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=46730"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46730\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46730"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46730"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}