[PHP-users 35003] Re: htmlタグの<と単なる<の区別

NiwaNobuo niwa @ kobuta.com
2010年 2月 15日 (月) 20:28:40 JST


 丹羽です。

お手を煩わせてしまいましたが、とても参考になりました。
いろいろと検討してみたいと思います。
タグを取り除くだけでも自分にとってはかなりの進歩です。

>うすきです。
>
>> うまい正規表現を考えれば、タグをとりのぞくことができるでしょうか?
>
>ちょっと作ってみました。
>
>$test = '<table summary="2>1" id="2>1" target="2>1">';
>//                  ↓受け付けるタグ
>if (preg_match('/<(a|table)((?:\s+[\w\-]+="[^"]*")*)>/', $test, $mch)) {
>    $nodeName = $mch[1];
>    $attributes = isset($mch[2])? $mch[2] : "";
>    $attributes = preg_replace_callback(
>        '/\s+(\w+)="([^"]*)"/', 'atrconv', $attributes
>    );
>
>    // テスト出力
>    echo '<pre>';
>    echo htmlspecialchars("<$nodeName$attributes>");
>    echo '</pre>';
>}
>function atrconv($mch) {
>    // ← 許可するタグと属性のチェック処理・サニタイズ
>    return " ". $mch[1].'="'. htmlspecialchars($mch[2]) .'"';
>}
>
>// 結果
><table summary="2&gt;1" id="2&gt;1" target="2&gt;1">
>
>HTML開きタグのみですが、こんな感じでブラッシュアップしていって、繰り返せ
>ばある程度までは簡単にいけるかも知れません。


PHP-users メーリングリストの案内