Code that PHP detects if the URL address and HTTP address format are valid

Source: Internet
Author: User
    1. /**
    2. DESC: Check 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 if the HTTP address is valid

    1. /**

    2. DESC: Detect If 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

Invocation Example:

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

    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.