用新行分解 PHP 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3997336/
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
Explode PHP string by new line
提问by Webnet
Simple, right? Well, this isn't working :-\
很简单吧?好吧,这不起作用:-\
$skuList = explode('\n\r', $_POST['skuList']);
回答by Larzan
Best Practice
最佳实践
As mentioned in the comment to the first answer, the best practice is to use the PHP constant PHP_EOLwhich represents the current system's EOL (End Of Line).
正如第一个答案的评论中提到的,最佳实践是使用 PHP 常量PHP_EOL,它代表当前系统的 EOL(行尾)。
$skuList = explode(PHP_EOL, $_POST['skuList']);
PHP provides a lot of other very useful constantsthat you can use to make your code system independent, see this linkto find useful and system independent directory constants.
PHP 提供了许多其他非常有用的常量,您可以使用它们使代码系统独立,请参阅此链接以查找有用且与系统无关的目录常量。
Warning
警告
These constants make your page system independent, but you might run into problems when moving from one system to another when you use the constants with data stored on another system. The new system's constants might be different from the previous system's and the stored data might not work anymore. So completely parse your data before storing it to remove any system dependent parts.
这些常量使您的页面系统独立,但是当您将这些常量用于存储在另一个系统上的数据时,您可能会在从一个系统移动到另一个系统时遇到问题。新系统的常量可能与以前系统的不同,并且存储的数据可能不再起作用。因此,在存储之前完全解析您的数据以删除任何系统相关部分。
UPDATE
更新
Andreas' comment made me realize that the 'Best Practice' solution I present here does not apply to the described use-case: the server's EOL (PHP) does not have anything to do with the EOL the browser (any OS) is using, but that (the browser) is where the string is coming from.
Andreas 的评论让我意识到我在此提供的“最佳实践”解决方案不适用于所描述的用例:服务器的 EOL (PHP) 与浏览器(任何操作系统)正在使用的 EOL 没有任何关系,但是(浏览器)是字符串的来源。
So please use the solution from @Alin_Purcaru (three down) to cover all your bases (and upvote his answer):
因此,请使用@Alin_Purcaru(三下)的解决方案来涵盖您的所有基础(并支持他的回答):
$skuList = preg_split('/\r\n|\r|\n/', $_POST['skuList']);
回答by Alin Purcaru
Cover all cases. Don't rely that your input is coming from a Windows environment.
涵盖所有情况。不要相信您的输入来自 Windows 环境。
$skuList = preg_split("/\r\n|\r|\n/", $_POST['skuList']);
or
或者
$skuList = preg_split('/\r\n|\r|\n/', $_POST['skuList']);
回答by Select0r
Try "\n\r"
(double quotes) or just "\n"
.
尝试"\n\r"
(双引号)或只是"\n"
.
If you're not sure which type of EOL you have, run a str_replace before your explode, replacing "\n\r" with "\n".
如果您不确定您拥有哪种类型的 EOL,请在爆炸前运行 str_replace,将“\n\r”替换为“\n”。
回答by Spudley
Lots of things here:
这里有很多东西:
- You need to use double quotes, not single quotes, otherwise the escaped characters won't be escaped.
- The normal sequence is
\r\n
, not\n\r
. - Depending on the source, you may just be getting
\n
without the\r
(or even in unusual cases, possibly just the\r
)
- 您需要使用双引号,而不是单引号,否则转义字符将不会被转义。
- 正常的顺序是
\r\n
, 不是\n\r
。 - 根据来源,您可能只是
\n
没有\r
(甚至在不寻常的情况下,可能只是\r
)
Given the last point, you may find preg_split()
using all the possible variants will give you a more reliable way of splitting the data than explode()
. But alternatively you could use explode()
with just \n
, and then use trim()
to remove any \r
characters that are left hanging around.
鉴于最后一点,您可能会发现preg_split()
使用所有可能的变体将为您提供比explode()
. 但是,您也可以explode()
仅使用with \n
, 然后使用trim()
删除任何\r
遗留下来的字符。
回答by iman
this php function explode string by newline
Attention: new line in Windowsis \r\nand in Linuxand Unixis \n
this function change all new lines to linuxmode then split it.
pay attention that empty lines will be ignored
这个 php 函数用换行符分解字符串
注意:Windows 中的新行是\r\n而在Linux和Unix 中是\n
这个函数将所有新行更改为linux模式然后拆分它。
注意空行会被忽略
function splitNewLine($text) {
$code=preg_replace('/\n$/','',preg_replace('/^\n/','',preg_replace('/[\r\n]+/',"\n",$text)));
return explode("\n",$code);
}
example
例子
$a="\r\n\r\n\n\n\r\rsalam\r\nman khobam\rto chi\n\rche khabar\n\r\n\n\r\r\n\nbashe baba raftam\r\n\r\n\r\n\r\n";
print_r( splitNewLine($a) );
output
输出
Array
(
[0] => salam
[1] => man khobam
[2] => to chi
[3] => che khabar
[4] => bashe baba raftam
)
回答by Oberdan
try
尝试
explode(chr(10), $_POST['skuList']);
回答by Andrew Sledge
For a new line, it's just
对于新的线路,它只是
$list = explode("\n", $text);
For a new line and carriage return (as in Windows files), it's as you posted. Is your skuList a text area?
对于换行和回车(如在 Windows 文件中),就像您发布的那样。你的 skuList 是一个文本区域吗?
回答by Russell Dias
Place the \n
in double quotes:
将\n
放在双引号中:
explode("\n", $_POST['skuList']);
explode("\n", $_POST['skuList']);
In single quotes, if I'm not mistaken, this is treated as \
and n
separately.
在单引号中,如果我没记错的话,这被视为\
和n
分开处理。
回答by piddl0r
Have you tried using double quotes?
您是否尝试过使用双引号?