PHP code to detect whether the URL address and http address format are valid

WBOY
Release: 2016-07-25 09:04:44
Original
1089 people have browsed it
  1. /**
  2. desc: Check whether the network address format is valid
  3. link: bbs.it-home.org
  4. date: 2013/2/24
  5. */
  6. function checkUrl($weburl)
  7. {
  8. return !ereg("^http(s)*://[_a-zA-Z0-9 -]+(.[_a-zA-Z0-9-]+)*$", $weburl);
  9. } ?>
Copy code

2. Determine whether the http address is valid

  1. /**

  2. desc: Check whether the http address is valid
  3. link: bbs.it-home.org
  4. date: 2013/2/24
  5. */
  6. function url_exists($url)
  7. {
  8. $ch = curl_init();
  9. curl_setopt($ch, CURLOPT_URL,$ url);
  10. curl_setopt($ch, CURLOPT_NOBODY, 1); // Do not download
  11. curl_setopt($ch, CURLOPT_FAILONERROR, 1);
  12. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  13. return (curl_exec($ch)!= =false) ? true : false;
  14. }

  15. //Method 2

  16. function img_exists($url)
  17. {
  18. return file_get_contents($url,0,null,0,1) ? true : false;
  19. }

  20. //Method 3:

  21. function url_exists($url)
  22. {
  23. $head = @get_headers($url);
  24. return is_array($head) ? true : false;
  25. } ?>

Copy code

Call example:

  1. $url='http://bbs.it-home.org';
  2. echo url_exists($url);
  3. ?>
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template