user3739032
user3739032

Reputation: 21

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.

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";
    }   

Upvotes: 0

Views: 3900

Answers (2)

Eddwin Paz
Eddwin Paz

Reputation: 2878

This Question has been answered several Times: remove html tags

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

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>   

Upvotes: 1

Kai
Kai

Reputation: 3823

HTML can't be parsed using just regular expressions: Using regular expressions to parse HTML: why not?

I recommend looking into using a library for HTML parsing. There are free ones out there, such as http://htmlpurifier.org/.

Upvotes: 0

Related Questions