php-tidyをインストールして htmlを整形する

  • 作成日 2020.08.30
  • php
php-tidyをインストールして htmlを整形する

php-tidyをインストールしてhtmlを整形するまでの手順となります。簡単なサンプルコードも記述してます。

環境

  • OS CentOS Linux release 8.1.1911 (Core)
  • PHP 7.4.5

※CentOs8 php7.4をインストールする手順はこちら

php-tidyインストール

下記のコマンドを実行してインストールします。

sudo dnf install php-tidy

apacheの再起動が必要なので、再起動します。

sudo systemctl restart httpd

php-tidy使い方

下記のコードはphp-tidyを使った簡単なサンプルとなります。

<?php
ini_set('display_errors', "On");

$html = '<p>テストHTML';
$config = array('doctype' => 'auto','indent' => true,'wrap' => 200);
//パースします。
$tidy = tidy_parse_string($html, $config, 'UTF8');
$h = tidy_get_html($tidy);
// 整形されたソース
$html = $h->value;
echo $html;

実行結果