I ran your script and got a problem with php:
Note: Undefined index: host
So, the variable $ url ['host'] does not exist ... If I var_dump the output in this case, the contents are returned:
array (size=3) 'scheme' => string 'https' (length=5) 'host' => string 'en.wikipedia.org' (length=16) 'path' => string '/wiki/Data' (length=10) array (size=1) 'path' => string 'data.gov.ua/' (length=12) ( ! ) Notice: Undefined index: host array (size=1) 'path' => string 'e-data.gov.ua/' (length=14) ( ! ) Notice: Undefined index: host
As you can see, the URL is interpreted as a path.
Outputs:
$urls[] = 'data.gov.ua/';
Error. Invalid URL$urls[] = '//data.gov.ua/';
Valid.$urls[] = 'http://data.gov.ua/';
Valid.
Advice. Use //
if you do not know if it is http or https.
By the way, you can simplify your code: p
function clean_url(array $urls) { $good_url = []; foreach( $urls as $url ) { // add a chech on the start of the url. $parse = parse_url($url); if( isset($url['host']) ) array_push($good_url, $url['host']); else $good_url[] = 'Invalid Url'; // for example, or triger error. } return $good_url; }
See foreach
and isset
source share