0

I have the following seems simple code in php; but the ptoblem is that it shows all valid links as "not valid"; any help appreciated:

<?php
   $m = "urllist.txt";
   $n = fopen($m, "r");
   while (!feof($n)) {
      $l = fgets($n);
      if (filter_var($l, FILTER_VALIDATE_URL) === FALSE) {
          echo "NOT VALID - $l<br>";
      } else {
          echo "VALID - $l<br>";
      }
   }
   fclose($n);
?>
9
  • How are the URLs saved in the file? If you have more than one URL in one line or some characters at the end of the line that should show you that now a new URL is, they will be always NOT VALID Commented Sep 4, 2014 at 5:37
  • I checked them in the same code one by one and they are valid; I checked the file and cleaned it; I went through this check, nothing seems wrong with the text file. Commented Sep 4, 2014 at 5:38
  • just echo the variable $l. then you can understand the type of url Commented Sep 4, 2014 at 5:43
  • it is already echo'ed; please check the code... Commented Sep 4, 2014 at 5:44
  • I tested the code right now. It works (half) if you use a URL like the URL of this site (this post) it say NOT VALID, but the second URL (google.com) is VALID Commented Sep 4, 2014 at 5:45

4 Answers 4

3

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 
Sign up to request clarification or add additional context in comments.

1 Comment

trim() is needed because while reading lines from text file, one space is appended at the end of each line.
0

maybe you have some symbols at end of each line '\n'

I think you can just use trim function before validate the $l like this:

filter_var(trim($l), FILTER_VALIDATE_URL) === TRUE

maybe this will help you.

Comments

0

Please try with the different filters available to see where it fails:

  • FILTER_FLAG_SCHEME_REQUIRED - Requires URL to be an RFC compliant URL (like http:// example)
  • FILTER_FLAG_HOST_REQUIRED - Requires URL to include host name (like http:// www.example.com)
  • FILTER_FLAG_PATH_REQUIRED - Requires URL to have a path after the domain name (like www. example.com/example1/test2/)
  • FILTER_FLAG_QUERY_REQUIRED - Requires URL to have a query string (like "example.php?name=Peter&age=37")

(cc of http://www.w3schools.com/php/filter_validate_url.asp)

You can try the good old regex too:

if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$url))

Comments

0

Try this code. It must be helpful. I have tested it and its working.

<?php
    $m = "urllist.txt";
    $n = fopen($m, "r");
    while (!feof($n)) {
        $l = fgets($n);
        if(filter_var(trim($l), FILTER_VALIDATE_URL)) {
            echo "URL is not valid";
        }
        else{
            echo "URL is valid";
        }
    }
    fclose($n);
?>

Here is the DEMO

4 Comments

It gave same results; my code gave the following results: NOT VALID - http//www.google.com NOT VALID - http//www.bing.com NOT VALID - http//www.yahoo.com NOT VALID - michaelking.ca NOT VALID - michaelking.ca NOT VALID - mekdam.com NOT VALID - mekdam.com VALID - oilpainting.mkzg.com Your code gave: URL is not validURL is not validURL is not validURL is not validURL is not validURL is not validURL is not validURL is valid
did you check the demo?
The demo is not the same code. The problem was with fgets().
Yeah that's right. I couldn't include the file in demo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.