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

[Osaka/Yokohama] 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”

Simple HTML DOM Parser でコメントアウト部分を消す方法

こんにちは!
Webシステム部の長谷です。

今回は、PHPでHTMLを解析することができる「Simple HTML DOM Parser」を使用して、
HTMLのコメント部分(<!-- -->)を除去する方法をご紹介します。

というのも、とある案件にてSimple HTML DOM Parserを使用して
HTMLの最適化を行っていたのですが、
その際、使い方など色々と調査をしていたのですが、
コメントに関しては触れられている方があまりいなかった(あまり使わないから?)ので
備忘録も兼ねてここに残しておこうと思った次第であります。

Simple HTML DOM Parserの導入

ライブラリのダウンロード

Simple HTML DOM Parser

マニュアル

PHP Simple HTML DOM Parser Manual

対象HTMLファイル

comment_out.html

<!DOCTYPE html>
<html lang="ja">
<head>
	<meta charset="utf-8">
	<title>株式会社ビヨンド</title>
</head>
<body>
	<!-- 会社名 -->
	<h1>株式会社ビヨンド</h1>

	<!-- ビヨンドの企業理念 -->
	<h2>「共に創り支え続ける」</h2>
	<h3>私たちビヨンドの仕事は、ITを通じて新しい価値を創造する人々を支えること。</h3>
	<p>それは決して華々しい仕事ではないが、ユーザーがより快適に・楽しく生活するためにはなくてはならない存在だと考えます。
	単なるシステムサービスのみではなく、創り手のビジネスチャンスを広げるための"人の輪"や"情報"を提供することにより、
	互いに成長できる関係を長期的に作りあげることを使命とします。</p>

	<!--
		上記の「会社名」「ビヨンドの企業理念」のように一行のコメントはもちろん
		このように複数行に渡って
		記述されているコメントも
		削除することが可能です
	-->
</body>
</html>

実行PHPの作成

ライブラリを読み込む

require_once 'simple_html_dom.php';

対象HTMLを読み込む

$html = file_get_html('comment_out.html');

コメント部分を抽出して除去

foreach ($html->find("comment") as $comment) {
	$comment->outertext = "";
}

$html->find("comment") でコメント部分を配列で抽出でき、
$comment->outertext = ""; で要素を空文字(除去)にできます。

保存する

$body = $html->save();
$html->clear();

ファイルに書き込む

file_put_contents("comment_out_delete.html", $body);

完成したPHPファイル

<?php
// ライブラリを読み込む
require_once 'simple_html_dom.php';

// 対象HTMLを読み込む
$html = file_get_html('comment_out.html');

// コメント部分を抽出して除去
foreach ($html->find("comment") as $comment) {
	$comment->outertext = "";
}

// 保存する
$body = $html->save();
$html->clear();

// ファイルに書き込む
file_put_contents("comment_out_delete.html", $body);
?>

PHP実行後のHTMLファイル

comment_out_delete.html

<!DOCTYPE html>
<html lang="ja">
<head>
	<meta charset="utf-8">
	<title>株式会社ビヨンド</title>
</head>
<body>
	
	<h1>株式会社ビヨンド</h1>

	
	<h2>「共に創り支え続ける」</h2>
	<h3>私たちビヨンドの仕事は、ITを通じて新しい価値を創造する人々を支えること。</h3>
	<p>それは決して華々しい仕事ではないが、ユーザーがより快適に・楽しく生活するためにはなくてはならない存在だと考えます。
	単なるシステムサービスのみではなく、創り手のビジネスチャンスを広げるための"人の輪"や"情報"を提供することにより、
	互いに成長できる関係を長期的に作りあげることを使命とします。</p>

	
</body>
</html>

正常にコメントが除去されましたね。

最後に

私が所属するシステム開発のサービスサイト「SEKARAKU Lab(セカラク ラボ)」を開設しました。
ビヨンドは、サーバーの設計・構築から運用までをワンストップでお任せいただけますので、サーバーサイド開発でお困りの方はお気軽にお問い合わせください。
SEKARAKU Lab:[https://sekarakulab.beyondjapan.com/](https://sekarakulab.beyondjapan.com/)

以上となります。

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
0
読み込み中...
0 票, 平均: 0.00 / 10
1,339
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[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

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

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