php中如何处理换行符?
我想知道是否有办法在 PHP 中操作换行符。例如,明确告诉在 explode()
函数中选择使用哪种换行符(LF、CRLF...)。
大概是这样的:
$rows = explode('<LF>', $list);
//<LF> here would be the line break
有人可以帮忙吗?谢谢 (:
I wanted to know if there's a way to manipulate line breaks within PHP. Like, to explicitly tell what kind of line break to select (LF, CRLF...) for using in an explode()
function for instance.
it would be something like that:
$rows = explode('<LF>', $list);
//<LF> here would be the line break
anyone can help? thanks (:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
LF和CR只是代码点0x0A(换行)和0x0D(回车 >) ASCII 格式。您可以按字面意思编写它们,也可以使用适当的转义序列:
请记住使用 双引号 为 单引号 只知道转义序列
\\
和\'
。CRLF 将只是两个字符的串联。因此:
如果您想在 CR 和 LF 处进行拆分,您可以使用正则表达式进行拆分:
并跳过空行(即多个换行符的序列):
LF and CR are just abbreviations for the characters with the code point 0x0A (LINE FEED) and 0x0D (CARRIAGE RETURN) in ASCII. You can either write them literally or use appropriate escape sequences:
Remember using the double quotes as single quotes do only know the escape sequences
\\
and\'
.CRLF would then just be the concatenation of both characters. So:
If you want to split at both CR and LF you can do a split using a regular expression:
And to skip empty lines (i.e. sequences of more than just one line break characters):
根据您的需要,我可以想到一些可能性:
"\r\n"
PHP_EOL
常量preg_split('/[\r\n]+/', ...)
file()
在分解之前对输入字符串进行标准化:
Some possibilities I can think of, depending on your needs:
"\r\n"
PHP_EOL
constantpreg_split('/[\r\n]+/', ...)
file()
Normalize the input string before exploding: