Jam*_*mes 45 php parsing textarea line
<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;
Run Code Online (Sandbox Code Playgroud)
如何:
1)从这个textarea()获取每一行$text并使用它们foreach()?
2)添加<br />到每行的末尾,除了最后一行?
3)将每一行抛到一个数组.
重要 - textarea中的文本可以是多语言.
试过用:
$text = str_replace('\n', '<br />', $text);
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
谢谢.
Jim*_* W. 96
该nl2br()会插入<br />换行符(前\n)和trim()将删除任何结局\n或空格字符.
$text = trim($_POST['textareaname']); // remove the last \n or whitespace character
$text = nl2br($text); // insert <br /> before \n
Run Code Online (Sandbox Code Playgroud)
那应该做你想要的.
UPDATE
下面的代码不起作用的原因是因为为了\n被识别,它需要在双引号内,因为双引号解析它们内部的数据,其中单引号从字面上理解,IE"\n"
$text = str_replace('\n', '<br />', $text);
Run Code Online (Sandbox Code Playgroud)
要修复它,它将是:
$text = str_replace("\n", '<br />', $text);
Run Code Online (Sandbox Code Playgroud)
但是使用nl2br()PHP提供的内置函数仍然更好.
编辑
对不起,我认为第一个问题是你可以添加换行符,实际上这会改变答案,因为任何类型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.
}
Run Code Online (Sandbox Code Playgroud)
如果你这样做,你需要<br />在自己完成处理之前将其附加到行的末尾,因为该explode()函数将删除\n字符.
添加array_filter()到trim()掉任何多余的\r字符可能是挥之不去的.
Sha*_*han 39
你可以使用PHP常量:
$array = explode(PHP_EOL, $text);
Run Code Online (Sandbox Code Playgroud)
附加说明:
1.对我来说,这是最简单,最安全的方式,因为它是跨平台兼容的(Windows/Linux等)
2.最好在任何时候使用PHP CONSTANT以便更快地执行
小智 8
旧胎......?好吧,有人可能碰到这个......
请查看http://telamenta.com/techarticle/php-explode-newlines-and-you
而不是使用:
$values = explode("\n", $value_string);
Run Code Online (Sandbox Code Playgroud)
使用更安全的方法,如:
$values = preg_split('/[\n\r]+/', $value_string);
Run Code Online (Sandbox Code Playgroud)
使用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);
Run Code Online (Sandbox Code Playgroud)
这输出:
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)
Run Code Online (Sandbox Code Playgroud)
这个对我有用:
if (isset($_POST['MyTextAreaName'])){
$array=explode( "\r\n", $_POST['MyTextAreaName'] );
Run Code Online (Sandbox Code Playgroud)
现在,我的 $array 将包含我需要的所有行
for ($i = 0; $i <= count($array); $i++)
{
echo (trim($array[$i]) . "<br/>");
}
Run Code Online (Sandbox Code Playgroud)
(确保if用另一个大括号关闭该块)
}
Run Code Online (Sandbox Code Playgroud)