php 从 textarea 获取每一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3702400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get each line from textarea
提问by James
<textarea> put returns between paragraphs
for linebreak add 2 spaces at end
indent code by 4 spaces
quote by placing > at start of line
</textarea>
$text = value from this textarea;
How to:
如何:
1) Get each line from this textarea ($text
) and work with them using foreach()
?
1) 从此 textarea ( ) 中获取每一行$text
并使用foreach()
?
2) Add <br />
to the end of each line, except the last one?
2) 添加<br />
到每一行的末尾,除了最后一行?
3) Throw each line to an array.
3) 将每一行扔到一个数组中。
Important- text inside textarea can be multilanguage.
重要- textarea 内的文本可以是多语言的。
Have tried to use:
曾尝试使用:
$text = str_replace('\n', '<br />', $text);
But it doesn't work.
但它不起作用。
Thanks.
谢谢。
回答by Jim W.
You will want to look into the nl2br()function along with the trim().
The nl2br()
will insert <br />
before the newline character (\n
) and the trim()
will remove any ending \n
or whitespace characters.
该nl2br()
会插入<br />
换行符(前\n
)和trim()
将删除任何终止\n
或空格字符。
$text = trim($_POST['textareaname']); // remove the last \n or whitespace character
$text = nl2br($text); // insert <br /> before \n
That should do what you want.
那应该做你想做的。
UPDATE
更新
The reason the following code will not work is because in order for \n
to be recognized, it needs to be inside double quotes since double quotes parse data inside of them, where as single quotes takes it literally, IE "\n"
以下代码不起作用的原因是为了\n
被识别,它需要在双引号内,因为双引号解析其中的数据,而单引号从字面上看,IE"\n"
$text = str_replace('\n', '<br />', $text);
To fix it, it would be:
要修复它,它将是:
$text = str_replace("\n", '<br />', $text);
But it is still better to use the builtin nl2br()
function, PHP provides.
但是还是使用nl2br()
PHP提供的内置函数更好。
EDIT
编辑
Sorry, I figured the first question was so you could add the linebreaks in, indeed this will change the answer quite a bit, as anytype of explode()
will remove the line breaks, but here it is:
抱歉,我认为第一个问题是您可以添加换行符,确实这会大大改变答案,因为任何类型的explode()
都会删除换行符,但这里是:
$text = trim($_POST['textareaname']);
$textAr = explode("\n", $text);
$textAr = array_filter($textAr, 'trim'); // remove any extra \r characters left behind
foreach ($textAr as $line) {
// processing here.
}
If you do it this way, you will need to append the <br />
onto the end of the line before the processing is done on your own, as the explode()
function will remove the \n
characters.
如果您这样做,您将需要<br />
在您自己完成处理之前将 附加到行尾,因为该explode()
函数将删除\n
字符。
Added the array_filter()
to trim()
off any extra \r
characters that may have been lingering.
加入array_filter()
到trim()
关闭任何额外的\r
可能已被挥之不去的字符。
回答by Shah Erhan
You could use PHP constant:
您可以使用 PHP 常量:
$array = explode(PHP_EOL, $text);
additional notes:
1. For me this is the easiest and the safest way because it is cross platform compatible (Windows/Linux etc.)
2. It is better to use PHP CONSTANT whenever you can for faster execution
附加说明:
1. 对我来说,这是最简单和最安全的方法,因为它是跨平台兼容的(Windows/Linux 等)
2. 最好尽可能使用 PHP CONSTANT 以加快执行速度
回答by Temitope
Old tread...? Well, someone may bump into this...
旧胎……?好吧,有人可能会碰到这个......
Please check out http://telamenta.com/techarticle/php-explode-newlines-and-you
请查看http://telamenta.com/techarticle/php-explode-newlines-and-you
Rather than using:
而不是使用:
$values = explode("\n", $value_string);
Use a safer method like:
使用更安全的方法,例如:
$values = preg_split('/[\n\r]+/', $value_string);
回答by profimedica
It works for me:
这个对我有用:
if (isset($_POST['MyTextAreaName'])){
$array=explode( "\r\n", $_POST['MyTextAreaName'] );
now, my $array will have all the lines I need
现在,我的 $array 将包含我需要的所有行
for ($i = 0; $i <= count($array); $i++)
{
echo (trim($array[$i]) . "<br/>");
}
(make sure to close the if
block with another curly brace)
(确保if
用另一个花括号关闭块)
}
回答by shamittomar
Use PHP DOM to parse and add <br/>
in it. Like this:
使用PHP DOM解析并添加<br/>
进去。像这样:
$html = '<textarea> put returns between paragraphs
for linebreak add 2 spaces at end
indent code by 4 spaces
quote by placing > at start of line
</textarea>';
//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('textarea');
//get text and add <br/> then remove last <br/>
$lines = $nodes->item(0)->nodeValue;
//split it by newlines
$lines = explode("\n", $lines);
//add <br/> at end of each line
foreach($lines as $line)
$output .= $line . "<br/>";
//remove last <br/>
$output = rtrim($output, "<br/>");
//display it
var_dump($output);
This outputs:
这输出:
string ' put returns between paragraphs
<br/>for linebreak add 2 spaces at end
<br/>indent code by 4 spaces
<br/>quote by placing > at start of line
' (length=141)
回答by Vincent Robert
$content = $_POST['content_name'];
$lines = explode("\n", $content);
foreach( $lines as $index => $line )
{
$lines[$index] = $line . '<br/>';
}
// $lines contains your lines
回答by Fribu - Smart Solutions
For a <br>
on each line, use
对于<br>
每一行的 a,使用
<textarea wrap="physical"></textarea>
You will get \n
s in the value of the textarea. Then, use the nl2br()
function to create <br>
s, or you can explode() it for <br>
or \n
.
您将\n
在 textarea 的值中获得s。然后,使用该nl2br()
函数创建<br>
s,或者您可以为<br>
or爆炸()它\n
。
Hope this helps
希望这可以帮助
回答by cichy
$array = explode("\n", $text);
for($i=0; $i < count($array); $i++)
{
echo $line;
if($i < count($array)-1)
{
echo '<br />';
}
}