如何爆炸多行字符串? [英] How to explode a multi-line string?

查看:106
本文介绍了如何爆炸多行字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在每一行上具有不同值的字符串:

I have a string that has different values on each line:

$matches="value1
value2
value3
value4
value5
";

我想将整个字符串分解为一个由各个值组成的数组.我知道如何爆炸以空格分隔的字符串,例如explode(' ', $matches).但是如何在这种类型的字符串上使用爆炸功能?

I want to explode the whole string in to an array consisting of the values separeted. I know how to explode a space separated string, like explode(' ', $matches). But how do i use the explode function on this type of string?

我尝试过:

$matches=explode('\n',$matches);
print_r($matches);

但结果是:

Array
(
    [0] => hello
hello
hello
hello
hello
hello
hello

)

推荐答案

您需要将'\n'更改为"\n".

来自 PHP.net :

如果字符串包含在其中 双引号(),PHP将解释 更多特殊的转义序列 字符:

If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters:

\ n换行符(LF或ASCII值为0x0A(10))
更多...

\n linefeed (LF or 0x0A (10) in ASCII)
More...

这篇关于如何爆炸多行字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆