Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7629097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:44:53+00:00 2026-05-31T05:44:53+00:00

I am attempting to create a php function which will check if the passes

  • 0

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:

  1. Overall URL length – May be a max of 30 charecters
  2. URL length after last ‘/’ – May be a max of 10 characters
  3. Number of ‘/’ after protocol (http://) – Max 2
  4. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:44:55+00:00Added an answer on May 31, 2026 at 5:44 am

    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:

    function _is_short_url($url){
            // 1. Overall URL length - May be a max of 30 charecters
            if (strlen($url) > 30) return false;
    
            $parts = parse_url($url);
    
            // No query string & no fragment
            if ($parts["query"] || $parts["fragment"]) return false;
    
            $path = $parts["path"];
            $pathParts = explode("/", $path);
    
            // 3. Number of '/' after protocol (http://) - Max 2
            if (count($pathParts) > 2) return false;
    
            // 2. URL length after last '/' - May be a max of 10 characters
            $lastPath = array_pop($pathParts);
            if (strlen($lastPath) > 10) return false;
    
            // 4. Max length of host
            if (strlen($parts["host"]) > 10) return false;
    
            return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to create a REST API in PHP and I'd like to
I'm attempting to create a jQuery version of php's wordwrap function -- I asked
I am attempting to create a UPnP method on an internal PHP server if
I am attempting to create a web page that will allow a user to
I am attempting to create a simple tracker using php and google maps. Right
I am using the php framework codeigniter. I am attempting to create a here
Hopefully this is something that will be easy to remedy. I'm having a bit
I am attempting to create a constructor for my controller that references a function
I am attempting to create a PHP SOAP connection to a .NET Web Service
I am attempting to create a php script that can connect thru ssh to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.