The string returned by fgets()
contains a trailing newline character that needs to be trimmed before you can validate it. Try out following code, I hope this will help you:
<?php
$m = "urllist.txt";
$n = fopen($m, "r");
while (!feof($n)) {
$l = fgets($n);
if(filter_var(trim($l), FILTER_VALIDATE_URL)) {
echo "VALID - $l<br>";
} else {
echo "NOT VALID - $l<br>";
}
}
fclose($n);
?>
I have tried with following urls:
http://stackoverflow.com/
https://www.google.co.in/
https://www.google.co.in/?gfe_rd=cr&ei=bf4HVLOmF8XFoAOg_4HoCg&gws_rd=ssl
www.google.com
http://www.example.com
example.php?name=Peter&age=37
and get following result:
VALID - http://stackoverflow.com/
VALID - https://www.google.co.in/
VALID - https://www.google.co.in/?gfe_rd=cr&ei=bf4HVLOmF8XFoAOg_4HoCg&gws_rd=ssl
NOT VALID - www.google.com
VALID - http://www.example.com
NOT VALID - example.php?name=Peter&age=37