{"id":1260,"date":"2025-10-20T19:27:32","date_gmt":"2025-10-20T11:27:32","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/1260.html"},"modified":"2025-10-20T19:29:16","modified_gmt":"2025-10-20T11:29:16","slug":"masterminds-html5-php-%e5%ba%93%e7%9a%84%e5%ae%89%e8%a3%85%e5%92%8c%e4%bd%bf%e7%94%a8%e7%9a%84%e8%af%a6%e7%bb%86%e8%af%b4%e6%98%8e","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/1260.html","title":{"rendered":"masterminds\/html5\u00a0PHP \u5e93\u7684\u5b89\u88c5\u548c\u4f7f\u7528\u7684\u8be6\u7ec6\u8bf4\u660e"},"content":{"rendered":"<p>\u4ee5\u4e0b\u662f\u5173\u4e8e <code>masterminds\/html5<\/code> PHP \u5e93\u7684\u5b89\u88c5\u548c\u4f7f\u7528\u7684\u8be6\u7ec6\u8bf4\u660e\uff1a<\/p><hr class=\"wp-block-separator has-alpha-channel-opacity\"><h3 class=\"wp-block-heading\"><strong>1. \u5e93\u7b80\u4ecb<\/strong><\/h3><p><code>masterminds\/html5<\/code> \u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790\u548c\u64cd\u4f5c HTML5 \u6587\u6863\u7684 PHP \u5e93\u3002\u5b83\u57fa\u4e8e PHP \u7684 DOM \u6269\u5c55\uff0c\u5e76\u9075\u5faa HTML5 \u89c4\u8303\uff0c\u80fd\u591f\u6b63\u786e\u5904\u7406\u73b0\u4ee3 HTML5 \u6807\u7b7e\u548c\u8bed\u4e49\u3002<\/p><hr class=\"wp-block-separator has-alpha-channel-opacity\"><h3 class=\"wp-block-heading\"><strong>2. \u5b89\u88c5\u6b65\u9aa4<\/strong><\/h3><h4 class=\"wp-block-heading\">\u901a\u8fc7 Composer \u5b89\u88c5<\/h4><ol class=\"wp-block-list\">\n<li>\u786e\u4fdd\u5df2\u5b89\u88c5 Composer\uff08PHP \u4f9d\u8d56\u7ba1\u7406\u5de5\u5177\uff09\u3002<\/li>\n\n\n\n<li>\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/li>\n<\/ol><pre class=\"wp-block-code\"><code>   composer require masterminds\/html5<\/code><\/pre><h4 class=\"wp-block-heading\">\u73af\u5883\u8981\u6c42<\/h4><ul class=\"wp-block-list\">\n<li>PHP 7.4 \u6216\u66f4\u9ad8\u7248\u672c<\/li>\n\n\n\n<li>\u542f\u7528 PHP \u7684 <code>dom<\/code> \u6269\u5c55\uff08\u901a\u5e38\u5df2\u5185\u7f6e\uff0c\u4f46\u9700\u68c0\u67e5 <code>php.ini<\/code>\uff09<\/li>\n\n\n\n<li>\u53ef\u9009\uff1a\u82e5\u9700\u5904\u7406\u6587\u4ef6\uff0c\u786e\u4fdd <code>php-xml<\/code> \u6269\u5c55\u5df2\u5b89\u88c5\uff08Linux \u7cfb\u7edf\u53ef\u8fd0\u884c <code>sudo apt-get install php-xml<\/code>\uff09<\/li>\n<\/ul><hr class=\"wp-block-separator has-alpha-channel-opacity\"><h3 class=\"wp-block-heading\"><strong>3. \u57fa\u672c\u7528\u6cd5<\/strong><\/h3><h4 class=\"wp-block-heading\">\u89e3\u6790 HTML5 \u5185\u5bb9<\/h4><pre class=\"wp-block-code\"><code>require 'vendor\/autoload.php';\nuse MastermindsHTML5;\n\n\/\/ \u521d\u59cb\u5316\u89e3\u6790\u5668\n$html5 = new HTML5();\n\n\/\/ \u89e3\u6790 HTML \u5b57\u7b26\u4e32\n$htmlString = '&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;body&gt;&lt;h1&gt;Hello World&lt;\/h1&gt;&lt;\/body&gt;&lt;\/html&gt;';\n$dom = $html5-&gt;loadHTML($htmlString);\n\n\/\/ \u89e3\u6790 HTML \u6587\u4ef6\n$dom = $html5-&gt;loadHTMLFile('path\/to\/your\/file.html');<\/code><\/pre><h4 class=\"wp-block-heading\">\u751f\u6210 HTML5 \u5185\u5bb9<\/h4><pre class=\"wp-block-code\"><code>\/\/ \u4ece DOMDocument \u5bf9\u8c61\u751f\u6210 HTML \u5b57\u7b26\u4e32\n$html = $html5-&gt;saveHTML($dom);\n\n\/\/ \u76f4\u63a5\u4fdd\u5b58\u5230\u6587\u4ef6\n$html5-&gt;saveHTMLFile($dom, 'path\/to\/output.html');<\/code><\/pre><hr class=\"wp-block-separator has-alpha-channel-opacity\"><h3 class=\"wp-block-heading\"><strong>4. \u9ad8\u7ea7\u7528\u6cd5<\/strong><\/h3><h4 class=\"wp-block-heading\">\u64cd\u4f5c DOM \u5143\u7d20<\/h4><pre class=\"wp-block-code\"><code>\/\/ \u521b\u5efa\u65b0\u5143\u7d20\n$newElement = $dom-&gt;createElement('p', 'This is a new paragraph.');\n$newElement-&gt;setAttribute('class', 'text-bold');\n\n\/\/ \u5c06\u5143\u7d20\u6dfb\u52a0\u5230 body \u4e2d\n$body = $dom-&gt;getElementsByTagName('body')-&gt;item(0);\n$body-&gt;appendChild($newElement);\n\n\/\/ \u8f93\u51fa\u4fee\u6539\u540e\u7684 HTML\necho $html5-&gt;saveHTML();<\/code><\/pre><h4 class=\"wp-block-heading\">\u9519\u8bef\u5904\u7406<\/h4><pre class=\"wp-block-code\"><code>try {\n    $dom = $html5-&gt;loadHTMLFile('invalid.html');\n} catch (Exception $e) {\n    echo \"\u89e3\u6790\u9519\u8bef: \" . $e-&gt;getMessage();\n}\n\n\/\/ \u68c0\u67e5\u89e3\u6790\u5668\u9519\u8bef\uff08\u5982\u975e\u81f4\u547d\u9519\u8bef\uff09\nif ($html5-&gt;hasErrors()) {\n    foreach ($html5-&gt;getErrors() as $error) {\n        echo \"\u8b66\u544a: $errorn\";\n    }\n}<\/code><\/pre><hr class=\"wp-block-separator has-alpha-channel-opacity\"><h3 class=\"wp-block-heading\"><strong>5. \u6ce8\u610f\u4e8b\u9879<\/strong><\/h3><ol class=\"wp-block-list\">\n<li><strong>HTML5 \u89c4\u8303\u517c\u5bb9\u6027<\/strong><br>\u8be5\u5e93\u9075\u5faa HTML5 \u6807\u51c6\uff0c\u53ef\u80fd\u5bf9\u65e7\u7248 HTML \u7684\u677e\u6563\u8bed\u6cd5\u62a5\u9519\u3002<\/li>\n\n\n\n<li><strong>\u6027\u80fd\u4f18\u5316<\/strong><br>\u89e3\u6790\u5927\u578b HTML \u6587\u4ef6\u65f6\u53ef\u80fd\u6d88\u8017\u8f83\u591a\u5185\u5b58\uff0c\u5efa\u8bae\u5206\u5757\u5904\u7406\u6216\u4f7f\u7528\u6d41\u5f0f\u89e3\u6790\uff08\u82e5\u5e93\u652f\u6301\uff09\u3002<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\u683c\u5f0f\u5316<\/strong><br>\u9ed8\u8ba4\u8f93\u51fa\u7684 HTML \u4f1a\u683c\u5f0f\u5316\u7f29\u8fdb\uff0c\u53ef\u901a\u8fc7\u9009\u9879\u5173\u95ed\uff1a<\/li>\n<\/ol><pre class=\"wp-block-code\"><code>   $html5 = new HTML5(['formatOutput' =&gt; false]);<\/code><\/pre><ol start=\"4\" class=\"wp-block-list\">\n<li><strong>\u7f16\u7801\u95ee\u9898<\/strong><br>\u786e\u4fdd\u8f93\u5165\u8f93\u51fa\u7684\u5b57\u7b26\u7f16\u7801\u4e00\u81f4\uff08\u5982 UTF-8\uff09\uff0c\u53ef\u5728\u89e3\u6790\u65f6\u6307\u5b9a\uff1a<\/li>\n<\/ol><pre class=\"wp-block-code\"><code>   $dom = $html5-&gt;loadHTML($htmlString, ['encoding' =&gt; 'UTF-8']);<\/code><\/pre><hr class=\"wp-block-separator has-alpha-channel-opacity\"><h3 class=\"wp-block-heading\"><strong>6. \u793a\u4f8b\u4ee3\u7801\u6c47\u603b<\/strong><\/h3><pre class=\"wp-block-code\"><code>require 'vendor\/autoload.php';\nuse MastermindsHTML5;\n\n\/\/ \u521d\u59cb\u5316\n$html5 = new HTML5();\n\n\/\/ \u89e3\u6790\u5e76\u4fee\u6539 HTML\n$dom = $html5-&gt;loadHTMLFile('input.html');\n$title = $dom-&gt;getElementsByTagName('title')-&gt;item(0);\n$title-&gt;nodeValue = 'New Title';\n\n\/\/ \u4fdd\u5b58\u7ed3\u679c\n$html5-&gt;saveHTMLFile($dom, 'output.html');<\/code><\/pre><hr class=\"wp-block-separator has-alpha-channel-opacity\"><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u96c6\u6210 <code>masterminds\/html5<\/code> \u5230 PHP \u9879\u76ee\u4e2d\uff0c\u5b9e\u73b0 HTML5 \u6587\u6863\u7684\u89e3\u6790\u3001\u64cd\u4f5c\u548c\u751f\u6210\u3002<\/p>","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u662f\u5173\u4e8e masterminds\/html5 PHP \u5e93\u7684\u5b89\u88c5\u548c\u4f7f\u7528\u7684\u8be6\u7ec6\u8bf4\u660e\uff1a 1. \u5e93\u7b80\u4ecb masterminds\/html5 \u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790\u548c\u64cd\u4f5c HTML5 \u6587\u6863\u7684 PHP \u5e93\u3002\u5b83\u57fa\u4e8e PHP \u7684 DOM \u6269\u5c55\uff0c\u5e76\u9075\u5faa HTML5 \u89c4\u8303\uff0c\u80fd\u591f\u6b63\u786e\u5904\u7406\u73b0\u4ee3 HTML5 \u6807\u7b7e\u548c\u8bed\u4e49\u3002 2. \u5b89\u88c5\u6b65\u9aa4 \u901a\u8fc7 Com..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[45],"tuisongtax":[],"class_list":["post-1260","my1js","type-my1js","status-publish","hentry","my1js2nav-symfony"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":""}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js\/1260","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/my1js"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=1260"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=1260"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=1260"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=1260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}