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.'" />';

以下是输入标签中的输出。

您拥有哪些品牌的单杯咖啡机?#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 回答