1. 関数概要
tidy_reset_config は、PHP の Tidy 拡張に設定されているすべてのオプションをデフォルト値にリセットする関数です。tidy_setopt() などで変更したオプションを一括で元に戻したい場面や、同一の tidy オブジェクトを異なる設定で使い回す際のクリーンアップに活用できます。
| 項目 | 内容 |
|---|---|
| 関数名 | tidy_reset_config |
| 所属拡張 | Tidy |
| 戻り値の型 | bool |
| 手続き型 / OOP | 手続き型(OOP 版: $tidy->resetConfig()) |
| PHP バージョン | PHP 5 以降 |
| 公式ドキュメント | https://www.php.net/manual/ja/tidy.resetconfig.php |
2. 構文
// 手続き型
tidy_reset_config(tidy $tidy): bool
// オブジェクト指向型
$tidy->resetConfig(): bool
パラメータ
| パラメータ | 型 | 説明 |
|---|---|---|
$tidy | tidy | リセット対象の tidy オブジェクト |
戻り値
| 結果 | 戻り値 |
|---|---|
| 成功 | true |
| 失敗 | false |
3. 動作概念図
┌──────────────────────────────────────────────────────────┐
│ tidy オブジェクト(カスタム設定済み) │
│ ┌─────────────────┬──────────────────────────────────┐ │
│ │ オプション名 │ 変更後の値 │ │
│ ├─────────────────┼──────────────────────────────────┤ │
│ │ indent │ true ← tidy_setopt で変更 │ │
│ │ wrap │ 120 ← tidy_setopt で変更 │ │
│ │ output-xhtml │ true ← tidy_setopt で変更 │ │
│ │ ... │ ... │ │
│ └─────────────────┴──────────────────────────────────┘ │
└─────────────────────┬────────────────────────────────────┘
│ tidy_reset_config($tidy)
▼
┌──────────────────────────────────────────────────────────┐
│ tidy オブジェクト(デフォルト設定に戻った) │
│ ┌─────────────────┬──────────────────────────────────┐ │
│ │ オプション名 │ デフォルト値 │ │
│ ├─────────────────┼──────────────────────────────────┤ │
│ │ indent │ false │ │
│ │ wrap │ 68 │ │
│ │ output-xhtml │ false │ │
│ │ ... │ ... │ │
│ └─────────────────┴──────────────────────────────────┘ │
└──────────────────────────────────────────────────────────┘
4. 基本的な使い方
<?php
$html = '<html><body><p>サンプル</p></body></html>';
$tidy = tidy_parse_string($html, ['indent' => true, 'wrap' => 120], 'UTF8');
// リセット前の設定を確認
echo "リセット前:" . PHP_EOL;
echo " indent : " . var_export(tidy_getopt($tidy, 'indent'), true) . PHP_EOL;
echo " wrap : " . tidy_getopt($tidy, 'wrap') . PHP_EOL;
// デフォルトにリセット
$result = tidy_reset_config($tidy);
echo PHP_EOL . "リセット結果: " . ($result ? '✅ 成功' : '❌ 失敗') . PHP_EOL;
// リセット後の設定を確認
echo PHP_EOL . "リセット後:" . PHP_EOL;
echo " indent : " . var_export(tidy_getopt($tidy, 'indent'), true) . PHP_EOL;
echo " wrap : " . tidy_getopt($tidy, 'wrap') . PHP_EOL;
出力例:
リセット前:
indent : true
wrap : 120
リセット結果: ✅ 成功
リセット後:
indent : false
wrap : 68
5. 実践的なコード例
例1: リセット前後の設定を比較表示するクラス
<?php
class TidyConfigResetChecker
{
private tidy $tidy;
/** @var string[] */
private array $targets = [
'indent', 'indent-spaces', 'wrap', 'tab-size',
'output-xhtml', 'char-encoding', 'clean', 'quiet',
];
public function __construct(string $html, array $config)
{
$this->tidy = tidy_parse_string($html, $config, 'UTF8');
}
public function printCurrent(string $label): void
{
echo "=== {$label} ===" . PHP_EOL;
foreach ($this->targets as $opt) {
$val = tidy_getopt($this->tidy, $opt);
printf(" %-20s : %s%s", $opt, var_export($val, true), PHP_EOL);
}
echo PHP_EOL;
}
public function reset(): bool
{
return tidy_reset_config($this->tidy);
}
}
$checker = new TidyConfigResetChecker(
'<html><body><p>テスト</p></body></html>',
[
'indent' => true,
'wrap' => 120,
'output-xhtml' => true,
'clean' => true,
'quiet' => true,
]
);
$checker->printCurrent('カスタム設定中');
$ok = $checker->reset();
echo "リセット: " . ($ok ? '✅ 成功' : '❌ 失敗') . PHP_EOL . PHP_EOL;
$checker->printCurrent('リセット後(デフォルト)');
出力例:
=== カスタム設定中 ===
indent : true
indent-spaces : 2
wrap : 120
tab-size : 8
output-xhtml : true
char-encoding : 'utf8'
clean : true
quiet : true
リセット: ✅ 成功
=== リセット後(デフォルト)===
indent : false
indent-spaces : 2
wrap : 68
tab-size : 8
output-xhtml : false
char-encoding : 'ascii'
clean : false
quiet : false
例2: OOP スタイルで resetConfig() を使う
<?php
class TidyOopConfigResetter
{
private tidy $tidy;
public function __construct(string $html, array $config = [])
{
$this->tidy = new tidy();
$this->tidy->parseString($html, $config, 'UTF8');
}
public function getOpt(string $opt): mixed
{
return $this->tidy->getOpt($opt);
}
public function reset(): bool
{
return $this->tidy->resetConfig();
}
public function compareReset(array $optNames): void
{
echo "=== リセット前後の比較 ===" . PHP_EOL;
$before = [];
foreach ($optNames as $opt) {
$before[$opt] = $this->getOpt($opt);
}
$this->reset();
foreach ($optNames as $opt) {
$after = $this->getOpt($opt);
$mark = $before[$opt] === $after ? ' (変化なし)' : ' ← リセット';
printf(
" %-20s: %s → %s%s%s",
$opt,
var_export($before[$opt], true),
var_export($after, true),
$mark,
PHP_EOL
);
}
}
}
$resetter = new TidyOopConfigResetter(
'<html><body><p>OOP テスト</p></body></html>',
['indent' => true, 'wrap' => 100, 'output-xhtml' => true, 'clean' => true]
);
$resetter->compareReset(['indent', 'wrap', 'output-xhtml', 'clean', 'tab-size']);
出力例:
=== リセット前後の比較 ===
indent : true → false ← リセット
wrap : 100 → 68 ← リセット
output-xhtml : true → false ← リセット
clean : true → false ← リセット
tab-size : 8 → 8 (変化なし)
例3: 同一オブジェクトを複数の設定で使い回すクラス
<?php
class TidyMultiConfigProcessor
{
private tidy $tidy;
public function __construct(private readonly string $html)
{
$this->tidy = tidy_parse_string($html, [], 'UTF8');
}
public function processWithConfig(string $label, array $config): string
{
// 前回の設定をリセットしてから新しい設定で再解析
tidy_reset_config($this->tidy);
$this->tidy = tidy_parse_string($this->html, $config, 'UTF8');
tidy_clean_repair($this->tidy);
$output = tidy_get_output($this->tidy);
echo "--- [{$label}] ({$this->describeConfig($config)}) ---" . PHP_EOL;
echo $output . PHP_EOL;
return $output;
}
private function describeConfig(array $config): string
{
return implode(', ', array_map(
fn($k, $v) => "{$k}=" . var_export($v, true),
array_keys($config),
$config
));
}
}
$processor = new TidyMultiConfigProcessor(
'<html><body><p>テキスト</p><br></body></html>'
);
$processor->processWithConfig('インデントあり・折り返し80', ['indent' => true, 'wrap' => 80]);
$processor->processWithConfig('インデントなし・折り返しなし', ['indent' => false, 'wrap' => 0]);
$processor->processWithConfig('XHTML出力', ['output-xhtml' => true, 'indent' => true]);
出力例:
--- [インデントあり・折り返し80] (indent=true, wrap=80) ---
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<p>テキスト</p>
<br>
</body>
</html>
--- [インデントなし・折り返しなし] (indent=false, wrap=0) ---
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<p>テキスト</p><br>
</body>
</html>
--- [XHTML出力] (output-xhtml=true, indent=true) ---
<?xml version="1.0" encoding="utf-8"?>
...
例4: リセットの成否を検証するクラス
<?php
class TidyResetVerifier
{
/** @var array<string, mixed> */
private array $defaults = [];
public function __construct(private tidy $tidy)
{
// デフォルト値を事前にスナップショット
$opts = ['indent', 'wrap', 'output-xhtml', 'char-encoding', 'clean', 'quiet'];
$base = tidy_parse_string('<html><body></body></html>', [], 'UTF8');
foreach ($opts as $opt) {
$this->defaults[$opt] = tidy_getopt($base, $opt);
}
}
public function verifyReset(): void
{
$ok = tidy_reset_config($this->tidy);
echo "resetConfig() 戻り値: " . ($ok ? '✅ true' : '❌ false') . PHP_EOL . PHP_EOL;
echo "=== デフォルト値との照合 ===" . PHP_EOL;
$allMatch = true;
foreach ($this->defaults as $opt => $defaultVal) {
$current = tidy_getopt($this->tidy, $opt);
$match = $current === $defaultVal;
$allMatch = $allMatch && $match;
$icon = $match ? '✅' : '❌';
printf(
" %s %-20s : 期待=%s / 実際=%s%s",
$icon,
$opt,
var_export($defaultVal, true),
var_export($current, true),
PHP_EOL
);
}
echo PHP_EOL . ($allMatch ? '✅ 全オプションがデフォルト値に戻りました。' : '❌ 一部オプションが一致しません。') . PHP_EOL;
}
}
$tidy = tidy_parse_string(
'<html><body><p>テスト</p></body></html>',
['indent' => true, 'wrap' => 200, 'output-xhtml' => true, 'clean' => true, 'quiet' => true],
'UTF8'
);
$verifier = new TidyResetVerifier($tidy);
$verifier->verifyReset();
出力例:
resetConfig() 戻り値: ✅ true
=== デフォルト値との照合 ===
✅ indent : 期待=false / 実際=false
✅ wrap : 期待=68 / 実際=68
✅ output-xhtml : 期待=false / 実際=false
✅ char-encoding : 期待='ascii' / 実際='ascii'
✅ clean : 期待=false / 実際=false
✅ quiet : 期待=false / 実際=false
✅ 全オプションがデフォルト値に戻りました。
例5: リセット→再設定を繰り返すパイプラインクラス
<?php
class TidyConfigPipeline
{
private tidy $tidy;
/** @var array<string, array> */
private array $presets = [
'minimal' => ['indent' => false, 'wrap' => 0, 'quiet' => true],
'readable' => ['indent' => true, 'wrap' => 80, 'quiet' => false],
'xhtml' => ['indent' => true, 'wrap' => 80, 'output-xhtml' => true],
'compact' => ['indent' => false, 'wrap' => 0, 'drop-empty-elements' => true],
];
public function __construct(private readonly string $html)
{
$this->tidy = tidy_parse_string($html, [], 'UTF8');
}
public function runAll(): void
{
foreach ($this->presets as $name => $config) {
// 毎回リセットしてから再適用
tidy_reset_config($this->tidy);
$this->tidy = tidy_parse_string($this->html, $config, 'UTF8');
tidy_clean_repair($this->tidy);
$output = tidy_get_output($this->tidy);
printf(
"[%s] indent=%-5s wrap=%-4s → %d bytes%s",
str_pad($name, 8),
var_export(tidy_getopt($this->tidy, 'indent'), true),
tidy_getopt($this->tidy, 'wrap'),
strlen($output),
PHP_EOL
);
}
}
}
$pipeline = new TidyConfigPipeline(
'<HTML><BODY><H1>見出し</H1><P>本文テキスト<br></P></BODY></HTML>'
);
$pipeline->runAll();
出力例:
[minimal ] indent=false wrap=0 → 140 bytes
[readable] indent=true wrap=80 → 195 bytes
[xhtml ] indent=true wrap=80 → 318 bytes
[compact ] indent=false wrap=0 → 138 bytes
例6: リセット後に特定オプションだけ再設定するクラス
<?php
class TidySelectiveReconfigurer
{
private tidy $tidy;
public function __construct(string $html, array $initialConfig = [])
{
$this->tidy = tidy_parse_string($html, $initialConfig, 'UTF8');
}
/**
* 一旦リセットしてから指定オプションだけ再設定する
* @param array<string, mixed> $overrides
*/
public function resetAndApply(array $overrides): bool
{
if (!tidy_reset_config($this->tidy)) {
return false;
}
foreach ($overrides as $opt => $value) {
if (!tidy_setopt($this->tidy, $opt, $value)) {
echo "⚠️ オプション設定失敗: {$opt}" . PHP_EOL;
}
}
return true;
}
public function getOutput(): string
{
tidy_clean_repair($this->tidy);
return tidy_get_output($this->tidy);
}
public function dumpOpts(array $optNames): void
{
foreach ($optNames as $opt) {
printf(" %-20s : %s%s", $opt, var_export(tidy_getopt($this->tidy, $opt), true), PHP_EOL);
}
}
}
$html = '<html><body><p>選択的再設定テスト</p></body></html>';
$reconfigurer = new TidySelectiveReconfigurer(
$html,
['indent' => true, 'wrap' => 120, 'output-xhtml' => true, 'clean' => true]
);
echo "初期設定:" . PHP_EOL;
$reconfigurer->dumpOpts(['indent', 'wrap', 'output-xhtml', 'clean']);
$reconfigurer->resetAndApply(['indent' => true, 'wrap' => 40]);
echo PHP_EOL . "リセット後(indent/wrapのみ再設定):" . PHP_EOL;
$reconfigurer->dumpOpts(['indent', 'wrap', 'output-xhtml', 'clean']);
echo PHP_EOL . "出力:" . PHP_EOL;
echo $reconfigurer->getOutput();
出力例:
初期設定:
indent : true
wrap : 120
output-xhtml : true
clean : true
リセット後(indent/wrapのみ再設定):
indent : true
wrap : 40
output-xhtml : false
clean : false
出力:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<p>選択的再設定テスト</p>
</body>
</html>
例7: リセット操作をログとして記録するクラス
<?php
class TidyConfigAuditLogger
{
/** @var array<int, array{time: string, opts_before: array, opts_after: array, success: bool}> */
private array $log = [];
/** @var string[] */
private array $watchedOpts = ['indent', 'wrap', 'output-xhtml', 'char-encoding', 'clean', 'quiet'];
public function resetAndLog(tidy $tidy): bool
{
$before = $this->snapshot($tidy);
$success = tidy_reset_config($tidy);
$after = $this->snapshot($tidy);
$this->log[] = [
'time' => date('Y-m-d H:i:s'),
'opts_before' => $before,
'opts_after' => $after,
'success' => $success,
];
return $success;
}
private function snapshot(tidy $tidy): array
{
$snap = [];
foreach ($this->watchedOpts as $opt) {
$snap[$opt] = tidy_getopt($tidy, $opt);
}
return $snap;
}
public function printLog(): void
{
foreach ($this->log as $i => $entry) {
echo "=== リセット #{$i} [{$entry['time']}] " .
($entry['success'] ? '✅ 成功' : '❌ 失敗') . " ===" . PHP_EOL;
foreach ($this->watchedOpts as $opt) {
$b = var_export($entry['opts_before'][$opt], true);
$a = var_export($entry['opts_after'][$opt], true);
$mark = $b !== $a ? ' ← 変化' : '';
printf(" %-20s: %s → %s%s%s", $opt, $b, $a, $mark, PHP_EOL);
}
echo PHP_EOL;
}
}
}
$tidy = tidy_parse_string(
'<html><body><p>ログテスト</p></body></html>',
['indent' => true, 'wrap' => 150, 'output-xhtml' => true, 'clean' => true, 'quiet' => true],
'UTF8'
);
$logger = new TidyConfigAuditLogger();
$logger->resetAndLog($tidy);
$logger->printLog();
出力例:
=== リセット #0 [2026-07-22 12:00:00] ✅ 成功 ===
indent : true → false ← 変化
wrap : 150 → 68 ← 変化
output-xhtml : true → false ← 変化
char-encoding : 'utf8' → 'ascii' ← 変化
clean : true → false ← 変化
quiet : true → false ← 変化
6. 関連関数との比較
| 関数名 | 用途 | 戻り値 |
|---|---|---|
tidy_reset_config() | 全オプションをデフォルト値にリセット | bool |
tidy_setopt() | 特定オプションの値を1件設定 | bool |
tidy_getopt() | 特定オプションの現在値を取得 | mixed |
tidy_get_config() | 全オプションの現在値を配列で取得 | array |
tidy_get_opt_doc() | オプションの説明文字列を取得 | string|false |
tidy_parse_string() | HTML 文字列を解析(第2引数で設定) | tidy|false |
使い分けのポイント: 設定を完全にゼロベースに戻したいときは
tidy_reset_config()、特定のオプションだけ変更したいときはtidy_setopt()を使います。
7. よくある落とし穴と注意点
① リセット後は tidy_parse_string() を再実行しないと反映されない
tidy_reset_config() はオプションをリセットするだけです。既に解析済みのドキュメントを新しい設定で整形したい場合は、リセット後に tidy_parse_string() を再実行してください。
// NG: リセットしても出力は変わらない
tidy_reset_config($tidy);
tidy_clean_repair($tidy);
echo tidy_get_output($tidy); // 以前の設定の出力になる
// OK: リセット後に再解析する
tidy_reset_config($tidy);
$tidy = tidy_parse_string($html, [], 'UTF8'); // 新設定(デフォルト)で再解析
tidy_clean_repair($tidy);
echo tidy_get_output($tidy);
② tidy_parse_string() に設定を渡すと tidy オブジェクト生成時に適用される
設定は tidy_parse_string() の第2引数で適用されます。tidy_reset_config() でリセットしても、次に tidy_parse_string() を呼ぶ際に再び設定を渡せば上書きされます。
③ デフォルト値は Tidy ライブラリのビルドに依存する
リセット後のデフォルト値は環境・バージョンによって異なる場合があります。デフォルト値を確認したい場合は、設定を渡さずに tidy_parse_string($html, [], 'UTF8') でオブジェクトを生成し、tidy_getopt() で確認してください。
④ 戻り値の false を見落とさない
tidy_reset_config() は失敗時に false を返します。重要な処理の前には戻り値を確認してください。
if (!tidy_reset_config($tidy)) {
throw new \RuntimeException('tidy_reset_config() に失敗しました。');
}
8. まとめ
| 項目 | 内容 |
|---|---|
| 主な用途 | tidy オブジェクトの全オプションをデフォルト値に一括リセットする |
| 戻り値 | true(成功)/ false(失敗) |
| OOP 版 | $tidy->resetConfig() |
| リセット後の再解析 | 新設定を反映するには tidy_parse_string() の再実行が必要 |
| デフォルト値の確認 | 設定なしで tidy_parse_string() を生成し tidy_getopt() で確認 |
| よく使う組み合わせ | tidy_setopt(), tidy_getopt(), tidy_get_config(), tidy_parse_string() |
tidy_reset_config() は「設定を白紙に戻す」ための関数です。同一の tidy オブジェクトを異なる設定で繰り返し使う場面や、設定の積み重なりを防いでクリーンな状態から処理を始めたいときに効果を発揮します。リセット単体では出力に影響しない点を理解した上で、tidy_parse_string() の再実行とセットで使うパターンを押さえておくと実践で迷わず活用できます。
