0
function URL ($url, $type)
{
    $regex = '_^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iuS';
    if($type == 'image') {
        $regex .= "";
    }

    if (preg_match($regex, $url)) {
        return true;
    } else {
        return false;
    }
}

With my function here i'm trying to validate a URL, And if i set the type to image to check if all the URL is an image or now, Then return true or false,

What i want to know is What do i add to my regex to check if the URL is for image or not, Then to return true in case the type is image and all the regex is matched to image

AXAI
  • 706
  • 6
  • 17
  • I don't think you can make this work. What happens with sites that don't show the resources' extension? I think imgur for example does that. What you need to do is something like sending a HEAD request and checking the `Content-Type` . – ishegg Sep 25 '17 at 19:31
  • I won't read a 500 character regex, but since you included `$` as the end of your regex, you won't be able to add additional matching characters (you can, but it will not ever match anything). – Imanuel Sep 25 '17 at 19:32
  • @ishegg doesn't imgur provide a direct url to the image itself? – AXAI Sep 25 '17 at 19:32
  • As what @ishegg said, there is no reliable way to determine a specific URI will return an image, because you cannot determine the mime-type/content-type based on the URI alone. Your best way is actually to [attempt to load the image using `get_headers`](https://stackoverflow.com/questions/12023950/get-content-type-of-requested-url-in-php), and see what you get in return. – Terry Sep 25 '17 at 19:32
  • You can't tell the file type from a URL. You need to request it and check the content-type. HEAD request should be enough. There are picture hosting sites that have links ending in .jpg but they give you a website with ads and the picture. – stesch Sep 25 '17 at 19:33
  • @Terry i'm aiming to check a direct url like this one `https://i.imgur.com/**********.jpg` – AXAI Sep 25 '17 at 19:35
  • 2
    Then just check if the URL ends with any of the known image extensions, e.g. `.*\.(jpe?g|png|tiff?|gif|bmp|svg|webp)$`. However be warned that this is **not an exhaustive check**: URLs without extensions might return an image, and URLs with an image might return a webpage. It is the server that determines what content to serve, and you cannot determine that just by eyeballing the URL alone. – Terry Sep 25 '17 at 19:35
  • @Terry So instead of checking the `.ext` it is better to check the `HEAD` request? – AXAI Sep 25 '17 at 19:37
  • Yes, because you can serve extension-less URLs. – Terry Sep 25 '17 at 19:37
  • @Terry Does using `HEAD` request to check the type uses bandwidth equal to downloading all the image? – AXAI Sep 25 '17 at 20:06

0 Answers0