我正在寻找有关从MYSQL数据库字段中删除HTML标签导出到excel时的帮助。
例如,当我导出时,我看到<BR>Testing actions and comments box
我的代码$file_ending = "xls";
//header info for browser
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=excel.xls");
header("Pragma: no-cache");
header("Expires: 0");
/*******Start of Formatting for Excel*******/
//define separator (defines columns in excel & tabs in word)
$sep = "'t"; //tabbed character
//start of printing column names as names of MySQL fields
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . "'t";
}
print("'n");
//end of printing column names
//start while loop to get data
while($row = mysql_fetch_row($result))
{
$schema_insert = "";
for($j=0; $j<mysql_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert = preg_replace("/'r'n|'n'r|'n|'r/", " ", $schema_insert);
$schema_insert .= "'t";
print(trim($schema_insert));
print "'n";
}
这个问题已经回答过好几次了:删除html标签
你可以在下面看到一些细节
基于PHP官方文档:http://www.php.net//manual/en/function.strip-tags.php
你应该使用条带标签:
<?php
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo "'n";
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>
输出:Test paragraph. Other text
<p>Test paragraph.</p> <a href="#fragment">Other text</a>
HTML不能使用正则表达式解析:使用正则表达式解析HTML:为什么不呢?
我建议考虑使用库进行HTML解析。也有免费的,比如http://htmlpurifier.org/。