Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP数组中的换行符_Php_Arrays_Forms_Line Breaks - Fatal编程技术网

PHP数组中的换行符

PHP数组中的换行符,php,arrays,forms,line-breaks,Php,Arrays,Forms,Line Breaks,我有三个HTML表单字段值name、saywords和mail,我试图用php将它们压缩并写入服务器上的一个txt文件中: 每个值都应该在txt字段中的新行上,这就是为什么我在数组中添加了\n。。。。我的错误在哪里 非常感谢你的帮助 $name = $_POST['name']; $saywords = $_POST['saywords']; $mail = $_POST['mail']; $data = array($name, . "\n" $mail, . "\n" $saywords

我有三个HTML表单字段值name、saywords和mail,我试图用php将它们压缩并写入服务器上的一个txt文件中:

每个值都应该在txt字段中的新行上,这就是为什么我在数组中添加了\n。。。。我的错误在哪里

非常感谢你的帮助

$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
我看不到数组的用法,您可以将它们连接起来,就像:

$data = $name . "\n" . $mail . "\n" . $saywords ;
我看不到数组的用法,您可以将它们连接起来,就像:

$data = $name . "\n" . $mail . "\n" . $saywords ;
你有两个问题:

$data应该是字符串而不是数组 . 连接左侧和右侧:abc。def变成abcdef。 先把圆点放进去\n甚至\n$mail在PHP中没有意义,因此您将得到一个解析错误。 将$data=行替换为$data=$name\n$邮寄\n$说话;你可以走了。

你有两个问题:

$data应该是字符串而不是数组 . 连接左侧和右侧:abc。def变成abcdef。 先把圆点放进去\n甚至\n$mail在PHP中没有意义,因此您将得到一个解析错误。
将$data=行替换为$data=$name\n$邮寄\n$说话;然后你就可以开始了。

当你使用html代码时使用

当你使用html代码时使用

这取决于服务器的操作系统。
请尝试\r\n而不是\n

这取决于服务器的操作系统。
请尝试\r\n而不是\n

好的,这是我的快照

/*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
顺便说一下,我强烈建议在将数据保存到txt文件之前,还要添加一些额外的验证。有了这段代码,有人可以很容易地通过无限制地发布大量数据来搞乱txt文件的内容

小贴士:

1仅接受长度有限的名称,且字符不允许使用特殊符号或换行符-您也可以在保存之前过滤掉它们

2验证已输入的电子邮件-如果格式正确,是否存在电子邮件地址域的mx记录,等等

3接受长度有限的单词,如果需要,拒绝或过滤掉特殊字符


通过这种方式,您将获得更干净的提交。

好的,这是我的照片

/*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
顺便说一下,我强烈建议在将数据保存到txt文件之前,还要添加一些额外的验证。有了这段代码,有人可以很容易地通过无限制地发布大量数据来搞乱txt文件的内容

小贴士:

1仅接受长度有限的名称,且字符不允许使用特殊符号或换行符-您也可以在保存之前过滤掉它们

2验证已输入的电子邮件-如果格式正确,是否存在电子邮件地址域的mx记录,等等

3接受长度有限的单词,如果需要,拒绝或过滤掉特殊字符


通过这种方式,您将获得更清晰的提交。

文件\u放置\u内容$file,内爆'\n',$data并从阵列中删除新行您为什么要创建阵列?只需执行$data=$name\n$邮寄\n$说话;完美的非常感谢!!:file\u put\u contents$file、内爆“\n”、$data并从阵列中删除新行为什么要创建阵列?只需执行$data=$name\n$邮寄\n$说话;完美的非常感谢!!:OP将数据保存在文本文件中,而不是输出,因此\n是正确的,不是我的否决票,顺便说一句。OP将数据保存在文本文件中,而不是输出,因此\n是正确的,不是我的否决票,顺便说一句。完美!谢谢!!完美的谢谢!!完美的谢谢!!完美的谢谢!!如果你看OP的代码,会有一些更大的问题。如果你看OP的代码,会有一些更大的问题。