Home > Net >  fputcsv() problem. Textarea isn't saved in one column
fputcsv() problem. Textarea isn't saved in one column

Time:12-06

Today, I found my limit in data operations knowledge.

I can successfully save input fields as well as textareas by doing this:

    $f = fopen($dir, "a");
    fputcsv($f, $content, ";");
    fclose($f);

Later on, I can retrieve the data and store it in array by using explode()... or so have I thought!

The problem is in me. I like BIG textareas. I am currently making a forum page and I would like to grant my visitors ability to break the line when creating a thread.

For instance:

 /*
   I break the line by pressing ENTER
   And again
*/

This is the code I wrote so far:

let's say, the input from the user looks like this

 --------------------------- 
| Hello,                    |
| I find this form amazing. |
|                           |
| Can I get a hug?          |
 --------------------------- 

The user pressed ENTER 3 times making this thread..

if(isset($_POST['thread']))
{
   $thread = htmlspecialchars($_POST['thread']);
   $date = date('d.m.Y');

   fclose($e);
   $f = fopen("../data/forum-data/threads.csv", "a");
   $data = array($user, $thread, $date, time(), $mail);
   fputcsv($f, $data, ";", chr(127));
   fclose($f);
   echo '<center><div >';
   echo 'SUCCESS!';
   echo '</center></div>';
                    
   $show_forum = true;
   //echo "Username: " . $user . "<br>" . "Email: " . $mail; 
}

in my threads.csv file, this is what's been saved:

someuser;Hello
I find this form amazing

can I get a hug?;05.12.2021;1638716270; [email protected]

This is wished:

someuser;Hello"< br >"I find this form amazing"< br >""< br >"can I get a hug?;05.12.2021;1638716270;   [email protected]

I want it to be saved in one row because the data cannot be retrieved at all. For example:

$o = fopen(../data/forum-data/threads.csv);
while(!feof($o))
{
  $row = fgets($o, 4096);
  $column = explode(";", $row);
  foreach($column as $element)
  {
    echo $element . "< br >";
  }
}
fclose($o);

Now, I assure you. The page is blank. Nothing gets printed out, unless the data is saved in a single row like in example from above. I am open to any solution that features php code. I have no idea about .js whatsoever.

Respectfully Regarding,

I am crying

CodePudding user response:

You can use str_replace() to replace any "\r\n" with a different text like <br />. Then you have the content in one line, which you can save via fputcsv(). The following example shows how to use str_replace():

<?php
$data = "some\r\ncontent\r\nover multiple\r\nlines";
$dataBr = str_replace("\r\n", "<br />", $data);

$fp = fopen('test.csv', 'w ');
fputcsv($fp, array('before', $dataBr, 'after'));
fclose($fp);

echo file_get_contents('test.csv');

This will generate the following output:

before,"some<br />content<br />over multiple<br />lines",after

As you see the new lines ("\r\n") has been replaced by the <br /> HTML element and the content is in one line.

  •  Tags:  
  • php
  • Related