I am attempting to create a php function which will check if the passes URL is a short URL. Something like this:
/**
* Check if a URL is a short URL
*
* @param string $url
* return bool
*/
function _is_short_url($url){
// Code goes here
}
I know that a simpler and a sure shot way would be to check a 301 redirect, but this function aims at saving an external request just for checking. Neither should the function check against a list of URL shortners as that would be a less scale-able approach.
So are a few possible checks I was thinking:
- Overall URL length – May be a max of 30 charecters
- URL length after last ‘/’ – May be a max of 10 characters
- Number of ‘/’ after protocol (http://) – Max 2
- Max length of host
Any thoughts on a possible approach or a more exhaustive checklist for this?
EDIT: This function is just an attempt to save an external request, so its ok to return true for a non-short url (but a real short one). Post passing through this function, I would anyways expand all short URLs by checking 301 redirects. This is just to eliminate the obvious ones.
I would not recommend to use regex, as it will be too complex and difficult to understand. Here is a PHP code to check all your constraints: