How to delete commented out parts with Simple HTML DOM Parser
Hello!
My name is Hase from the Web Systems Department.
This time, we will use "Simple HTML DOM Parser", which can parse HTML with PHP, to
parse the comment part of HTML ( ) will be introduced.
is because I was optimizing HTML
using Simple HTML DOM Parser for a certain project at that time I was doing a lot of research on how to use it, but
I couldn't find any comments. Since there weren't many people using it (maybe because they don't use it much?),
I thought I'd leave it here as a memorandum.
Introducing Simple HTML DOM Parser
Download 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> "Creating together and continuing to support"</h2><h3> Our job at Beyond is to support people who create new value through IT.</h3><p> Although it is not a glamorous job, we believe that it is indispensable for making users' lives more comfortable and enjoyable. Our mission is to create long-term relationships that allow for mutual growth by providing not only system services but also a "circle of people" and "information" that expand business opportunities for creators. </p><!-- 上記の「会社名」「ビヨンドの企業理念」のように一行のコメントはもちろん このように複数行に渡って 記述されているコメントも 削除することが可能です --></body></html>
Creating executable PHP
load library
require_once 'simple_html_dom.php';
Load the target HTML
$html = file_get_html('comment_out.html');
Extract and remove comment part
foreach ($html->find("comment") as $comment) { $comment->outertext = ""; }
You can extract the comment part as an array with
$html->find("comment")
and make the element empty (remove) with $comment->outertext = "";
save
$body = $html->save(); $html->clear();
write to file
file_put_contents("comment_out_delete.html", $body);
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> "Creating together and continuing to support"</h2><h3> Our job at Beyond is to support people who create new value through IT.</h3><p> Although it is not a glamorous job, we believe that it is indispensable for making users' lives more comfortable and enjoyable. Our mission is to create long-term relationships that allow for mutual growth by providing not only system services but also a "circle of people" and "information" that expand business opportunities for creators.</p></body></html>
The comment has been 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.