PHP的问题爆炸,试图分离一个字符串

时间:2019-06-18 10:23:07

标签: php explode

该字符串有问题

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)

3 个答案:

答案 0 :(得分: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()用于从数组中删除空,空值索引

答案 1 :(得分:0)

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

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

比起简单地使用已经尝试过的爆炸:

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

答案 2 :(得分:0)

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

$str ="12312312312312312 12312312312312312 12312312312312312 12312312312312312 12312312312312312 12312312312312312 12312312312312312";
var_dump(explode(" ",$str));