简体   繁体   English

从MYSQL导出到Excel PHP时删除HTML标记

[英]Remove HTML tags when exporting from MYSQL to Excel PHP

I"m looking for help related to removing HTML tags from a field in MYSQL database field when exporting to excel. 我正在寻找与导出到Excel时从MYSQL数据库字段中的字段删除HTML标签相关的帮助。

fore exaple when I export I see 我出口时的前肢,我看到

<BR>Testing actions and comments box

My Code 我的密码

$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 can't be parsed using just regular expressions: Using regular expressions to parse HTML: why not? 无法仅使用正则表达式来解析HTML:使用正则表达式来解析HTML:为什么不呢?

I recommend looking into using a library for HTML parsing. 我建议您考虑使用库进行HTML解析。 There are free ones out there, such as http://htmlpurifier.org/ . 那里有免费的,例如http://htmlpurifier.org/

This Question has been answered several Times: remove html tags 多次回答该问题: 删除html标签

But a little bit detailed you can see below. 但是,您可以在下面看到一些详细信息。

Based on the Official PHP Documentation : http://www.php.net//manual/en/function.strip-tags.php 基于官方PHP文档: http : //www.php.net//manual/zh/function.strip-tags.php

You should use Strip Tags: 您应该使用剥离标签:

<?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>');
?>

OutPut: 输出:

Test paragraph. Other text
<p>Test paragraph.</p> <a href="#fragment">Other text</a>   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM