Expand url list in php

I am trying to import urls from a list of urls using explode function.

For example, let's say

<?php 

$urls = "http://storage.google.com/gn-0be5doc/da7f835a8c249109e7a1_solr.txt
http://google.com/gn-0be5doc/1660ed76f46bfc2467239e_solr.txt
http://google.com/gn-0be5doc/6dffbff7483625699010_solr.txt
http://google.com/gn-0be5doc/ef246266ee2e857372ae5c73_solr.txt
http://google.com/gn-0be5doc/d0565363ec338567c79b54e6_solr.txt
http://google.com/gn-0be5doc/43bd2d2abd741b2858f2b727_solr.txt
http://google.com/gn-0be5doc/eb289a45e485c38ad3a23bc4726dc_solr.txt"; 

$url_array = explode (" ", $urls); 

?> 

      

Given that there is no delimeter here, the break functions return all of the text together.

Is there a way to get them separately? Maybe use the end of the url as part of the txt?

Thanks in advance.

+3


source to share


2 answers


looks like what you need:

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

or

$urls = explode( "\r\n", $urls );

      

if you can use http://

If it was a line with breaks, then:



$urls = "http://storage.google.com/gn-0be5doc/da7f835a8c249109e7a1_solr.txthttp://google.com/gn-0be5doc/1660ed76f46bfc2467239e_solr.txthttp://google.com/gn-0be5doc/6dffbff7483625699010_solr.txthttp://google.com/gn-0be5doc/ef246266ee2e857372ae5c73_solr.txt";

$urls = preg_split('@(?=http://)@', $urls);

print_r($urls);

      

not used as it removes the separator

+4


source


You clearly have line breaks in your code, and for line breaks with / without extra whitespace, you can use PHP_EOL

as a separator:



$url_array = explode(PHP_EOL, $urls);

      

+1


source







All Articles