[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

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

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> "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.

If you found this article helpful , please give it a like!
0
Loading...
0 votes, average: 0.00 / 10
1,531
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

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 services and systems that run on browsers, such as web services, digital content, and business management systems) and game APIs (development of programs that communicate with application games).

We also develop private/custom apps for Shopify.

Originally worked at the Osaka office, but transferred to the Yokohama office in 2019.
His hobbies are baseball/karaoke/anime