How to delete commented out parts with Simple HTML DOM Parser

table of contents
Hello!
This is Hase from the Web Systems Department.
This time, we will use "Simple HTML DOM Parser" which can parse HTML in PHP to extract
the HTML comment part ( Here's how to remove it.
The reason is that I was using Simple HTML DOM Parser to
optimize HTML for a certain project, and
while I was doing some research on how to use it, I
didn't see many people mentioning it in the comments (perhaps because I don't use it much?), so I
thought I'd leave it here as a memo.
Introducing the Simple HTML DOM Parser
Download the library
manual
PHP Simple HTML DOM Parser Manual
Target HTML file
comment_out.html
<!DOCTYPE html><html lang="ja"><head><meta charset="utf-8"><title>Beyond Co., Ltd.</title></head><body><!-- 会社名 --><h1> Beyond Co., Ltd.</h1><!-- ビヨンドの企業理念 --><h2> "Continuing to create and support together"</h2><h3> Our job at Beyond is to support people who create new value through IT.</h3><p> It may not be a glamorous job, but we believe it is essential for users to live more comfortable and enjoyable lives.Our mission is to build long-term relationships that allow for mutual growth by providing not only system services but also "connections" and "information" that will expand business opportunities for creators. </p><!-- 上記の「会社名」「ビヨンドの企業理念」のように一行のコメントはもちろん このように複数行に渡って 記述されているコメントも 削除することが可能です --></body></html>
Creating Executable PHP
Loading a Library
require_once 'simple_html_dom.php';
Load the target HTML
$html = file_get_html('comment_out.html');
Extract and remove the comment part
foreach ($html->find("comment") as $comment) { $comment->outertext = ""; }
You can extract the comment part as an array using
$html->find("comment") and you can empty the element (remove it) using $comment->outertext = "";
Save
$body = $html->save(); $html->clear();
Write to a file
file_put_contents("comment_out_delete.html", $body);
The completed PHP file
<?php // ライブラリを読み込む require_once 'simple_html_dom.php'; // 対象HTMLを読み込む $html = file_get_html('comment_out.html'); // コメント部分を抽出して除去 foreach ($html-> find("comment") as $comment) { $comment->outertext = ""; } // Save $body = $html->save(); $html->clear(); // Write to file file_put_contents("comment_out_delete.html", $body); ?>
HTML file after PHP execution
comment_out_delete.html
<!DOCTYPE html><html lang="ja"><head><meta charset="utf-8"><title>Beyond Co., Ltd.</title></head><body><h1> Beyond Co., Ltd.</h1><h2> "Continuing to create and support together"</h2><h3> Our job at Beyond is to support people who create new value through IT.</h3><p> It may not be a glamorous job, but we believe it is essential for users to live more comfortable and enjoyable lives.Our mission is to build long-term relationships that allow for mutual growth by providing not only system services but also "connections" and "information" that will expand business opportunities for creators.</p></body></html>
The comment was successfully removed
lastly
I have opened the system development service site "SEKARAKU Lab" to which I belong.
Beyond is a one-stop service for everything from server design and construction to operation, so if you have any trouble with server-side development, please feel free to contact us.
SEKARAKU Lab: [https://sekarakulab.beyondjapan.com/](https://sekarakulab.beyondjapan.com/)
That's all.
0