Kristaps Gaidulis
Kristaps Gaidulis

Reputation: 104

Removing/ignoring html tags in csv file

been having this problem for a while now, I found many topics about this problem, but they don't really help me.

So Im creating 2 .csv files with this code :

<?php 
include 'cnn.php';
$result = mysql_query("SELECT * FROM www_jrpic_lv.web_notikumi_laiki");

//web_notikumi_laiki un web_notikumi izvade

$data = fopen('web_notikumi.csv', 'w');
$data2= fopen('web_notikumi_laiki.csv','w');

//Output Column Headings
fputcsv($data,array('ID Laiks','ID Notikums','Notikuma Laiks','Notikuma beigu laiks'));
fputcsv($data2,array('ID_not','Notikuma apraksts','Notikuma atbildīgais','Notikuma atskats','Baneris','Klase','Nosaukums','Piederība','piev.laiks','red.laiks','ID Tips','Titulbilde','Vieta'));

//Retrieve the data from database
$rows = mysql_query('SELECT * FROM web_notikumi_laiki');
$rows2 = mysql_query('SELECT * FROM web_notikumi');

//Loop through the data to store them inside CSV

while($row = mysql_fetch_assoc($rows)){
    fputcsv($data, $row);
    }
while($row2=mysql_fetch_assoc($rows2)){
    fputcsv($data2,$row2);
}
echo '<p>web_notikumi dati ir ievadīti';
echo '<p>web_notikumi_laiki dati ir ievadīti';
fclose($data);
fclose($data2);
exit();
?>

Data from table web_notikumi_laiki works well for me, but data in table web_notikumi contains lots of <p> tags . And after I use web_notikumi.csv file to fill table in other server it creates new entry after every <p> tag so i get ~12 000 entries instead of ~2000. My question : Can I remove those tags before I write it to .csv file? If not what options I have.

Upvotes: 0

Views: 3431

Answers (1)

feeela
feeela

Reputation: 29932

You can use strip_tags() to remove any HTML tags from a string. If some HTML elements are allowed, you can whitelist those in the second argument.

// for a single column from the query result
fputcsv( $data, strip_tags( $row['column_name'] ) );

// for all columns in the query result
fputcsv( $data, array_map( 'strip_tags', $row ) );

See: http://php.net/strip_tags

Upvotes: 2

Related Questions