Anonymous Anonymous - 2 years ago 470
PHP Question

how to explode string into double quote and separate by comma php?

I want to run multi url at the same time, but I don't want to copy and paste it again and again into an array. I want to put it all as a string and explode it in to double quote with comma just like we write in an array.

I want from this

$urls = "
http://
http://
http://
http://";


Into this

$urls = array("http://","http://","http://","http://");


This is what I have done so far

<?php
$urls = "
http://
http://
http://";
$url = preg_match_all('/"(?:\\\\.|[^\\\\"])*"|\S+/', $urls, $matches);
foreach($url as $ul){
echo $ul[1];
}
?>

Answer Source
<?php
$urls = "
    http://
    http://
    http://
    http://";

var_dump(array_map(function($s){return trim($s);},explode(PHP_EOL,trim($urls))));

Explode the array on a newline, first trimming off whitespace from the beginning and end. Then pass this array through array_map where each element is trimmed again to remove the whitespace from each element.