pear
foto:ファイル:Pears.jpg - Wikipedia
PHPでhtmlpurifierというフィルタクラスがあります。
wordpress などでは、似たようなもので、kses を使っていますが、もうすでにメンテされていない気配も、
htmlpurifier は、pear でインストールできるので、簡単便利。
two commands in your shell: pear channel-discover htmlpurifier.org pear install hp/HTMLPurifier
include("HTMLPurifier.includes.php"); include("HTMLPurifier/Filter/YouTube.php"); include("HTMLPurifier/Filter/ExtractStyleBlocks.php"); $document = 'htmlドキュメント'; $config = HTMLPurifier_Config::createDefault(); //規則 $config->set('HTML', 'Doctype', 'XHTML 1.0 Transitional'); //文字コード 言語 $config->set('Core', 'Encoding', 'UTF-8'); $config->set('Core', 'Language', 'ja'); //自動パラグラフ追加 2改行で<p> $config->set('AutoFormat', 'AutoParagraph', false); //html tidy htmlコード自動修正 $config->set('HTML', 'TidyLevel', 'medium'); //キャッシュ $config->set('Cache', 'SerializerPath', 'パスを指定' ); //許可エレメント //$config->set('HTML', 'AllowedElements', 'a,em,p,blockquote,h3'); //許可属性 $config->set('HTML', 'AllowedAttributes', 'a.href,a.title,a.id,p.id,a.rel,span.style,div.style'); //上とは反対に不許可エレメント $config->set('HTML', 'ForbiddenElements', 'b,i,pre,object,'); //id属性の管理 //Namespacing IDs $config->set('Attr', 'EnableID', true); $config->set('Attr', 'IDPrefix', 'user_'); //HTMLPurifier_Filterを継承したサブクラスで、フィルタして、youtubeのobjectタグパターンを発見したら、コードブロックをリプレースして書き換えも出来るよ。 //$config->set('Filter', 'YouTube', true); //$config->set('Filter', 'ExtractStyleBlocks', true); $h = new HTMLPurifier($config); echo $h->purify($document);
キャッシュ使ったほうがいいです。
csstidy も必要そうです。