仕事の一環というわけでもないですが、まあ一環的にtidyのオプションに関するリファレンスを読んでいる中で発見。
HTML Tidy Configuration Options Quick Reference
% php -r 'echo tidy_repair_string("あ", "", "utf8") . "\n";'
Warning: tidy_repair_string(): Could not load configuration file '' in Command line code on line 1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title></title>
</head>
<body>
あ
</body>
</html>
んー、なんか前に同じような事をどこかで書いた気がしますが、まあ面白い発見という事で。ちなみに、ncrをno(false)にしたら置き換えられずに、&がHTMLエスケープされます。
% php -r 'echo tidy_repair_string("あ", array("ncr"=>false), "utf8") . "\n";'
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title></title>
</head>
<body>
&#12354;
</body>
</html>
というような事を、今更ながら調べてます。
書いた後でもう少し読み進めたので補足します。
- ncrで数値実体参照の置き換えをコントロール出来るらしい
- アンパサンドのエスケープはquote-ampersandオプションでコントロールしてるらしい
% php -r 'echo tidy_repair_string("あ", array("ncr"=>false, "quote-ampersand" => false), "utf8") . "\n";'
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title></title>
</head>
<body>
あ
</body>
</html>
ついでに、バージョンも書いておきます。
% php -v PHP 5.2.0-8+etch7 (cli) (built: Jul 3 2007 00:42:49) Copyright (c) 1997-2006 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies % php -i |grep tidy /etc/php5/cli/conf.d/tidy.ini tidy Extension Version => 2.0 ($Id: tidy.c,v 1.66.2.8.2.15 2006/10/02 07:58:13 bjori Exp $) tidy.clean_output => no value => no value tidy.default_config => no value => no value
(2007/08/03追記)
16進数の場合も問題なく変換出来るようです。
$ php -v
PHP 5.2.3 (cli) (built: Jun 8 2007 00:28:41)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
$ php -r 'echo tidy_repair_string("&#x" . dechex(12354) . ";", array(), "utf8") . "\n";'
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title></title>
</head>
<body>
あ
</body>
</html>
ついでに数値実体参照を文字に変換する関数をメモ(16進数でも10進数でも関係なく変換出来るはず)。
<?php /** * @require tidy */ $str = mb_convert_encoding("あかさたな", 'EUC-JP', 'UTF-8'); echo ncr2chr($str, 'EUC-JP') . "\n"; /** * 入力文字列に含まれる数値実体参照を実体に変換する * * @param string $str 入力文字列 * @param string $inEnc 入力文字列のエンコーディング * @param string $outEnc 出力文字列のエンコーディング * @return string 出力文字列 * */ function ncr2chr($str, $inEnc = 'UTF-8', $outEnc = 'UTF-8') { $conf = array( 'show-body-only' => true, 'ncr' => true, 'quote-ampersand' => false, ); $str_utf8 = mb_convert_encoding($str, 'UTF-8', $inEnc); $str_ent = tidy_repair_string($str_utf8, $conf, 'utf8'); return mb_convert_encoding($str_ent, $outEnc, 'UTF-8'); } ?>

