Php code used to check whether the URL address and http address format are valid

Source: Internet
Author: User
Php code used to check whether the URL address and http address format are valid

  1. /**
  2. Desc: Check whether the network address format is valid
  3. Link: bbs.it-home.org
  4. Date:
  5. */
  6. Function checkUrl ($ weburl)
  7. {
  8. Return! Ereg ("^ http (s) *: // [_ a-zA-Z0-9-] + (. [_ a-zA-Z0-9-] +) * $", $ weburl );
  9. }?>

2. determine whether the http address is valid

  1. /**

  2. Desc: checks whether the http address is valid
  3. Link: bbs.it-home.org
  4. Date:
  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. }?>

Call example:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.