Skip to content

Instantly share code, notes, and snippets.

@chinhvo
Forked from thagxt/check-if-valid-url.php
Created March 20, 2024 08:00
Show Gist options
  • Save chinhvo/d3547a75d343d9f5f94f37d2246a38b5 to your computer and use it in GitHub Desktop.
Save chinhvo/d3547a75d343d9f5f94f37d2246a38b5 to your computer and use it in GitHub Desktop.
check if url is valid
<?php
/* @ http://stackoverflow.com/a/12628971 */
function isValidUrl($url){
// first do some quick sanity checks:
if(!$url || !is_string($url)){
return false;
}
// quick check url is roughly a valid http request: ( http://blah/... )
if( ! preg_match('/^http(s)?:\/\/[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(\/.*)?$/i', $url) ){
return false;
}
// the next bit could be slow:
if(getHttpResponseCode_using_curl($url) != 200){
return false;
}
// all good!
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment