如何在换行符上拆分多行字符串? [重复]

问题描述 投票:0回答:3

我的这个字符串有问题

string(313) "
12312312312312312
12312312312312312
12312312312312312
12312312312312312
12312312312312312
12312312312312312
12312312312312312
12312312312312312
12312312312312312
12312312312312312"

我想要的结果是=

array("12312312312312312","12312312312312312")
等等...

我想将条目分开放入数组中,我用 php 尝试了所有这些方法:

$numbers = explode("\n",$numbers); 

$numbers = explode(" ",$numbers);

$numbers = explode("<br>",$numbers);  

$numbers = str_replace("\n", " ", "<br>", $numbers)
php newline explode multiline
3个回答
2
投票

您需要

explode()
换行(
PHP_EOL
):

$numbers= array_filter(explode(PHP_EOL, $numbers)); // PHP_EOL used for new line

print_r($numbers);

输出:-https://3v4l.org/0OH4N

注意:-

array_filter()
用于从数组中删除空、空值索引


0
投票

首先用简单的空格替换任何换行元素,例如喜欢

$numbers = trim(preg_replace('/\s+/', ' ', $numbers));

您可以简单地使用您已经尝试过的爆炸:

$numbers = explode(" ",$numbers);

0
投票

您可以按此顺序初始化字符串。 这可能对你有帮助

$str ="12312312312312312 12312312312312312 12312312312312312 12312312312312312 12312312312312312 12312312312312312 12312312312312312";
var_dump(explode(" ",$str));
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.