Problem with php explode, trying to separate a string

Viewed 100

im having a problem with this string

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

The result i want is = array("12312312312312312","12312312312312312") etc...

I want to separate the entrys to put into thearray, i tryied all this methods with php:

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

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

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

$numbers= str_replace("\n", " ", "<br>", $numbers)
3 Answers

You need to explode() with new line (PHP_EOL):

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

print_r($numbers);

Output:-https://3v4l.org/0OH4N

Note:- array_filter() used to remove empty,null value indexes from array

First replace any newline element with a simple space e.g. like

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

Than you can simply use the explode you've already tried:

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

You can initialize the string in this order. This may help you

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