从textarea获取每一行

问题描述 投票:45回答:8
<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;

如何:

1)从这个textarea($text)获取每一行并使用foreach()与他们合作?

2)将<br />添加到每行的末尾,除了最后一行?

3)将每一行抛到一个数组。

重要 - textarea中的文本可以是多语言。


试过用:

$text = str_replace('\n', '<br />', $text);

但它不起作用。


谢谢。

php parsing textarea line
8个回答
95
投票

您将需要查看nl2br()函数以及trim()

nl2br()将在换行符(<br />)之前插入\n,而trim()将删除任何结尾的\n或空格字符。

$text = trim($_POST['textareaname']); // remove the last \n or whitespace character
$text = nl2br($text); // insert <br /> before \n 

那应该做你想要的。

UPDATE

下面的代码不起作用的原因是因为为了识别\n,它需要在双引号内,因为双引号解析它们内部的数据,其中单引号从字面上理解,IE "\n"

$text = str_replace('\n', '<br />', $text);

要修复它,它将是:

$text = str_replace("\n", '<br />', $text);

但是,使用PHP提供的内置nl2br()函数仍然更好。

编辑

对不起,我认为第一个问题是你可以添加换行符,确实这会改变答案很多,因为任何类型的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. 
} 

如果你这样做,你需要在处理完成之前将<br />附加到行的末尾,因为explode()函数将删除\n字符。

array_filter()添加到任何额外的trim()角色\r可能已经挥之不去。


38
投票

你可以使用PHP常量:

$array = explode(PHP_EOL, $text);

补充说明: 1.对我来说,这是最简单,最安全的方式,因为它是跨平台兼容的(Windows / Linux等) 2.只要你能更快地执行,最好使用PHP CONSTANT


8
投票

旧胎......?好吧,有人可能碰到这个......

请查看http://telamenta.com/techarticle/php-explode-newlines-and-you

而不是使用:

$values = explode("\n", $value_string);

使用更安全的方法,如:

$values = preg_split('/[\n\r]+/', $value_string);

4
投票

使用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);

这输出:

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)

4
投票

这个对我有用:

if (isset($_POST['MyTextAreaName'])){
    $array=explode( "\r\n", $_POST['MyTextAreaName'] );

现在,我的$数组将拥有我需要的所有行

    for ($i = 0; $i <= count($array); $i++) 
    {
        echo (trim($array[$i]) . "<br/>");
    }

(确保用另一个大括号关闭if块)

}

3
投票
$content = $_POST['content_name'];
$lines = explode("\n", $content);

foreach( $lines as $index => $line )
{
    $lines[$index] = $line . '<br/>';
}

// $lines contains your lines

3
投票

对于每行的<br>,请使用

<textarea wrap="physical"></textarea>

您将获得textarea值的\ns。然后,使用nl2br()函数创建<br>s,或者你可以为<br>\n爆炸()它。

希望这可以帮助


2
投票
$array = explode("\n", $text);
for($i=0; $i < count($array); $i++)
{
    echo $line;
    if($i < count($array)-1)
    {
         echo '<br />';
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.