php 从textarea获取每行[重复]

ve7v8dk2  于 10个月前  发布在  PHP
关注(0)|答案(8)|浏览(58)

此问题在此处已有答案

Split string by new line characters(19回答)
4天前关闭。

<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)从这个文本区域($text)中获取每一行,并使用foreach()处理它们?
2)将<br />添加到每一行的末尾,除了最后一行?
3)将每一行都抛出数组。
重要-文本区域内的文本可以是多语言的。

已尝试用途:

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


但它不起作用。

  • 谢谢-谢谢
3j86kqsm

3j86kqsm1#

**警告:**本答案集中在OP的特定部分,与题目无关。关于“如何获取每一行”这个问题的答案,请考虑下面的帖子。

您将需要查看nl2br()函数沿着trim()函数。
nl2br()将在换行符(\n)之前插入<br />trim()将删除任何结尾\n或空白字符。

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

字符串
你想怎么样就怎么样吧。

更新

下面的代码无法工作的原因是,为了识别\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字符。

bnlyeluc

bnlyeluc2#

你可以使用PHP常量:

$array = explode(PHP_EOL, $text);

字符串
附加说明:
1.对我来说,这是最简单和最安全的方法,因为它是跨平台兼容的(Windows/Linux等)
2.为了更快的执行,最好在任何时候都使用PHPCONSTANT

h43kikqp

h43kikqp3#

旧胎面...?嗯,有人可能会撞到这个……
请查看http://telamenta.com/techarticle/php-explode-newlines-and-you
而不是使用:

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

字符串
使用更安全的方法,如:

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

b5lpy0ml

b5lpy0ml4#

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

km0tfn4u

km0tfn4u5#

它对我有效:

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

字符串
现在,我的$数组将包含我需要的所有行

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


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

}

iszxjhcz

iszxjhcz6#

$array = explode("\n", $text);
for($i=0; $i < count($array); $i++)
{
    echo $line;
    if($i < count($array)-1)
    {
         echo '<br />';
    }
}

字符串

v64noz0r

v64noz0r7#

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

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

// $lines contains your lines

字符串

wpcxdonn

wpcxdonn8#

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

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

字符串
你将得到\n s在文本区域的值。然后,使用nl2br()函数创建<br> s,或者可以将其分解为<br>\n
希望这对你有帮助

相关问题