Reputation: 55
I created a code in php that performs a search on twitter and saves the result (100 tweets ) in a database . In this code , I also have the option to select all database tweets and exports them to a csv file.
However, if the tweet has a line break he'll be that way in csv :
What do I do to save this tweet on one line of the csv ( delete line break )
This is my code that exports tweets to csv :
// Database Connection
$host="xxxxxx";
$uname="xxxxx";
$pass="xxxxxx";
$database = "xxxxxx";
$connection=mysql_connect($host,$uname,$pass);
echo mysql_error();
//or die("Database Connection Failed");
$selectdb=mysql_select_db($database) or die("Database could not be selected");
$result=mysql_select_db($database)
or die("database cannot be selected <br>");
// Fetch Record from Database
$output = "";
$table = "tabela_tweets_novo"; // Enter Your Table Name
$sql = mysql_query("select tweet from $table");
$columns_total = mysql_num_fields($sql);
// Get The Field Name
for ($i = 0; $i < $columns_total; $i++) {
$heading = mysql_field_name($sql, $i);
$output .= '"'.$heading.'",';
}
$output .="\n";
// Get Records from the table
while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$acentua = utf8_decode($row["$i"]);
$output .='"'.$acentua.'",';
}
$output .="\n";
}
// Download the file
$filename = "UUXPost.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
echo $output;
exit;
Upvotes: 0
Views: 100
Reputation: 3161
You should use PHP's built-in csv methods that'll handle escaping for you (you'll have to escape fields with linebreaks). Sadly I'm on my mobile right now so it's quite a pita to add the urls. I'll do that later (or some editor).
Search the PHP manual for fputcsv().
Upvotes: 0
Reputation: 15057
Insert the 3 Line in your Code and change the replace string to what you want
$acentua = utf8_decode($row["$i"]); <- your code
$order = array("\r\n", "\n", "\r");
$replace = '<br />';
$acentua = str_replace($order, $replace, $acentua);
$output .='"'.$acentua.'",'; <- your code
Upvotes: 1