How to remove commented out parts with Simple HTML DOM Parser

Hello!
This is Hase from the Web Systems Department.

This time, we'll use "Simple HTML DOM Parser," which can parse HTML using PHP, to analyze
the HTML comment section (<!-- -->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 researching how to use it, I noticed that not many
people were mentioning comments (perhaps because they're not used often?), so I
thought I'd leave a record of it here as a memo for myself.

Introducing the Simple HTML DOM Parser

Download the library

Simple HTML DOM Parser

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 = ""; }

$html->find("comment") You can extract the comment section as an array using
string ("";) and you can remove the element by setting it to an empty

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 launched "SEKARAKU Lab," a service site for the system development company I belong to.
Beyond offers a one-stop service for everything from server design and construction to operation, so please feel free to contact us if you have any problems with server-side development.
SEKARAKU Lab:[https://sekarakulab.beyondjapan.com/](https://sekarakulab.beyondjapan.com/)

That's all

If you found this article helpful,please give it a "Like"!
0
Loading...
0 votes, average: 0.00 / 10
1,901
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Tatsuya Hase

Joined Beyond Co., Ltd. as a new graduate

We develop web systems (development of browser-based services and systems such as web services, digital content, and business management systems) and game APIs (development of programs for communication with app games)

We also develop private/custom apps for Shopify

Originally worked at the Osaka office, but transferred to the Yokohama office in 2019.
Hobbies: baseball, karaoke, anime.