简体   繁体   English

HTML换行符如何在PHP中呈现?

[英]how is HTML newline rendered in PHP?

I have a <textarea> in which I enter multiple numbers, one on each line(by pressing enter to go to new line in the text area). 我有一个<textarea> ,其中输入多个数字,每行一个(通过按Enter进入文本区域的新行)。 I receive these numbers on my server side after form submission. 提交表单后,我会在服务器端收到这些号码。 Let's call the <textarea> 's input 'X' . 我们将<textarea>的输入称为'X' When I echo 'X' to the browser, the numbers in ' X' are separated by space. 当我回声'X'到浏览器,在“的数字X'是由空格隔开。 I know that HTML doesn't support '\\n' character. 我知道HTML不支持'\\n'字符。 But, on my server side, I want to use explode() function to get all numbers in 'X' as separate elements of an array. 但是,在服务器端,我想使用explode()函数来获取'X'所有数字作为数组的单独元素。 Now, I am not able to find the delimiter which should be used in explode() . 现在,我无法找到应该在explode()使用的定界符。 I tried using '\\n' as delimiter but it doesn't separate the numbers after using explode() . 我尝试使用'\\n'作为分隔符,但在使用explode()之后,它不会分隔数字。 Thus, I am not using the right delimiter. 因此,我没有使用正确的定界符。 So, my question is 'what is the delimiter I should use and how is a HTML new line character(made by pressing enter in <textarea> ) rendered in PHP ? 因此,我的问题是“我应该使用什么定界符,以及如何在PHP呈现HTML新行字符(通过按<textarea> enter制成)? Please remember my purpose is not echoing on browser, my purpose is to separate the numbers using explode() 请记住我的目的不是在浏览器上回显,我的目的是使用explode()分隔数字

You must use "\\n" instead of '\\n'. 您必须使用“ \\ n”而不是“ \\ n”。 This is because only inside of " the special characters are converted to their real character codes. They stay as they are inside of ' though. 这是因为仅在“内”将特殊字符转换为它们的真实字符代码。尽管它们保留在“内”。

See the following example code: 请参见以下示例代码:

<?php
header('content-type: text/plain');
$test = 'test\ntest';
echo $test;
/*
test\ntest
*/

$test = "test\ntest";
echo $test;
/*
test
test
/*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM