Skip to content
Advertisement

fputcsv() problem. Textarea isn’t saved in one column

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 class="success-box">';
   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; someone@nix.com

This is wished:

someuser;Hello"< br >"I find this form amazing"< br >""< br >"can I get a hug?;05.12.2021;1638716270;   someone@nix.com

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

Advertisement

Answer

You can use str_replace() to replace any "rn" 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 = "somerncontentrnover multiplernlines";
$dataBr = str_replace("rn", "<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 ("rn") has been replaced by the <br /> HTML element and the content is in one line.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement