CoderGirlbyday
CoderGirlbyday

Reputation: 27

PHP Line break in array

I have three HTML form field values (name, saywords, mail) that I try to concat in php and write into one single txt file on my server:

Each value should be on a new line in the txt field, as is why I added the "\n" in the array .... where's my error?

Thanks a lot for any help!

$name = $_POST['name'];
$saywords = $_POST['saywords'];
$mail = $_POST['mail'];

$data = array($name,  . "\n" $mail, . "\n" $saywords);

file_put_contents("$t.$ip.txt",$data); // Will put the text to file

Upvotes: 1

Views: 3133

Answers (5)

Edgars WEBHAUS
Edgars WEBHAUS

Reputation: 84

Okay, so here is my shot.

/*first of all, you should always check if posted vars are actually set
and not empty. for that, you can use an universal function "empty", which
checks if the variable is set / not null / not an empty string / not 0. 
In such way you will avoid PHP warnings, when some of these variables 
will not be set*/

        $name = !empty($_POST['name']) ? $_POST['name'] : '';
        $saywords = !empty($_POST['saywords']) ? : $_POST['saywords'] : '';;
        $mail = !empty($_POST['mail']) ? $_POST['mail'] : '';

/*Secondly, do not use \n, \r, \r\n, because these are platform specific. 
Use PHP_EOL constant, it will do the job perfectly, by choosing 
what type of line-break to use best.

    As others mentioned - in your scenario, the string would be better solution. 
Add everything into string, and then put its contents into file. Avoid using 
double quotes, when you define PHP strings, and use single quotes instead - for 
performance and cleaner code.

    */


        $data = 'Name: '.$name.PHP_EOL.'E-Mail: '.$mail.PHP_EOL.'Message: '.$saywords.PHP_EOL.PHP_EOL;


        file_put_contents($t.$ip.'.txt', $data); // Will put the text to file

By the way, I strongly suggest to also add some extra validation, before saving data to that txt file. With this code somebody can easily mess up contents of your txt file, by posting huge amounts of data with no limits.

Tips:

1) Accept only Names with limited lengths and charaters (do not allow to use special symbols or line breaks - you can also filter them out, before saving)

2) Validate e-mail which has been entered - if it is in correct format, does mx records exists for the domain of e-mail address, and so on...

3) Accept "saywords" with limited length, and if needed - deny or filter out special characters.

You will get much cleaner submissions by doing this way.

Upvotes: 0

Mwangi Njuguna
Mwangi Njuguna

Reputation: 75

It depends with the operating system of the server. Try "\r\n" instead of "\n"

Upvotes: 1

Gooner
Gooner

Reputation: 369

Use <br /> as you use html code

Upvotes: -1

Keiji
Keiji

Reputation: 1042

You have two problems:

  • $data should be a string not an array
  • . concatenates the left hand side and the right hand side: "abc" . "def" becomes "abcdef".
    • putting the dot first like in . "\n" or even . "\n" $mail doesn't make sense in PHP so you'll get a parse error.

Replace your $data = line with $data = $name . "\n" . $mail . "\n" . $saywords; and you'll be good to go.

Upvotes: 3

Taki
Taki

Reputation: 17654

i don't see the use of array , you can concatenate them just like :

$data = $name . "\n" . $mail . "\n" . $saywords ;

Upvotes: 3

Related Questions