0

HTMLタグ、スタイルおよびスクリプトタグのコンテンツも削除したいのですが、コードがスタイルタグのコンテンツを削除していません。理由がわかりません。これについて何か考えはありますか?

$search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript 
               '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags 
               '@<style[^>]*?>.*?</style>@si',    // Strip style tags properly 
               '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments including CDATA 
               ); 

$htmlstring = 'Which brand(s) of single serve coffee brewer do you own? <style type="text/css"> #answer67627X49X1159other {display:none;}</style>';
$htmlstring .= '<style> #answer67627X49X1159999 {display:none;}</style><script>alert(123);</script>';

$htmlstring = preg_replace($search,'',$htmlstring);

echo '<input style="width:90%" type="text" value="'.$htmlstring.'" />';

以下は、inputタグの出力です。

あなたはどのブランドのシングルサーブコーヒーメーカーを所有していますか?#answer67627X49X1159other {display:none;}#answer67627X49X1159999 {display:none;}

4

2 に答える 2

0

パターンの順序が悪い

<?php
$search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript 
               '@<style[^>]*?>.*?</style>@si',
               '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags 
               '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments including CDATA 
               ); 

$htmlstring = 'Which brand(s) of single serve coffee brewer do you own? <style type="text/css"> #answer67627X49X1159other {display:none;}</style>';
$htmlstring .= '<style> #answer67627X49X1159999 {display:none;}</style><script>alert(123);</script>';

$htmlstring = preg_replace($search, '' ,$htmlstring);
var_dump($htmlstring);

// string(57) "Which brand(s) of single serve coffee brewer do you own? "
于 2012-11-07T12:03:30.650 に答える
0

スタイルタグに到達する前に、すでにhtmlタグを削除しています。スクリプトとスタイルが残りの前に処理されるように、置換の順序を変更します

$search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript                
                '@<style[^>]*?>.*?</style>@si',    // Strip style tags properly 
                '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags 
                '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments including CDATA 
           ); 
于 2012-11-07T12:03:40.297 に答える