我有一个带有html文本区域的表单.我想在php中获取此文本区域的内容,以便每行可以存储在一个数组中.我尝试使用'/ n'进行内爆.但它不起作用.我怎样才能做到这一点.
这是我的代码
$notes = explode('/n',$_POST['notes']);
Run Code Online (Sandbox Code Playgroud)
你需要使用这个:
$notes = explode("\n", $_POST['notes']);
Run Code Online (Sandbox Code Playgroud)
(反斜杠,不是正斜杠,双引号而不是单引号)
只有当行以\n结尾时,Palantir的解决方案才有效(Linux默认行结束).
例如.
$text = "A\r\nB\r\nC\nD\rE\r\nF";
$splitted = explode( "\n", $text );
var_dump( $splitted );
Run Code Online (Sandbox Code Playgroud)
将输出:
array(5) {
[0]=>
string(2) "A "
[1]=>
string(2) "B "
[2]=>
string(1) "C"
[3]=>
string(4) "D E "
[4]=>
string(1) "F"
}
Run Code Online (Sandbox Code Playgroud)
如果没有,你应该使用这个:
$text = "A\r\nB\r\nC\nD\rE\r\nF";
$splitted = preg_split( '/\r\n|\r|\n/', $text );
var_dump( $splitted );
Run Code Online (Sandbox Code Playgroud)
或这个:
$text = "A\r\nB\r\nC\nD\rE\r\nF";
$text = str_replace( "\r", "\n", str_replace( "\r\n", "\n", $text ) );
$splitted = explode( "\n", $text );
var_dump( $splitted );
Run Code Online (Sandbox Code Playgroud)
我认为最后一个会更快,因为它不使用正则表达式.
例如.
$notes = str_replace(
"\r",
"\n",
str_replace( "\r\n", "\n", $_POST[ 'notes' ] )
);
$notes = explode( "\n", $notes );
Run Code Online (Sandbox Code Playgroud)