如何爆炸多行字符串?

问题描述:

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

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...