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

  • SEARCH
  • Home
  • 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 5985229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:29:59+00:00 2026-05-22T22:29:59+00:00

I have been searching for the best way to validate a URL in php

  • 0

I have been searching for the best way to validate a URL in php and decided to use both regex and filter_val() I would like to share my code and get some feedback please.

function _valid_urls($str) {

        $regex = "/^(http):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i";

        if(!filter_var($str, FILTER_VALIDATE_URL) || (!preg_match($regex, $str))) //if invalid URL
        {               
            return FALSE;
        }
        else 
        {
            return TRUE;
        }
    } 

The code works but I’m not entirely sure if it’s secure.

EDIT:

I found the most efficient regex for website URLs to be /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \?=.-]*)*\/?$/

http://www.catswhocode.com/blog/10-regular-expressions-for-efficient-web-development

  • 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-22T22:29:59+00:00Added an answer on May 22, 2026 at 10:29 pm

    You’ve made a few errors in the regex. Nothing fatal, I don’t think but nevertheless, just a few miscellaneous things you can do to clean it up. You have put parentheses around http, and they don’t need to be there. It looks like you’re not capturing it for use later. If you’re trying to make the http:// part optional, you’ll want to use (?:http:\/\/)? instead. Also, note that you need to escape backslashes in a string. Would this work just as well?

    $regex = "/^".
      "(?:http:\\/\\/)?".  // Look for http://, but make it optional.
      "(?:[A-Z0-9][A-Z0-9_-]*(?:\\.[A-Z0-9][A-Z0-9_-]*))". // Server name
      "(?:\\d+)?".         // Optional port number
      "(?:\\/\\.*)?/i";    // Optional training forward slash and page info
    

    There are probably better regexes out there for matching URLs. I’d suggest Googling regex url and having a look at them. Don’t reinvent the wheel if you don’t have to! Also note that the above doesn’t allow for URLs without top-level domains, such as http://localhost/mypage.html. If you put a question mark before the double quotes on the “Server name” line, that should allow non-tld URLs.

    This is a bit inefficient:

    if(!filter_var($str, FILTER_VALIDATE_URL) || (!preg_match($regex, $str))) //if invalid URL
    {               
        return FALSE;
    }
    else 
    {
        return TRUE;
    }
    

    Your expression will yield a true/false value. How about just returning that, negating if needed?

    return !(!filter_var($str, FILTER_VALIDATE_URL) || !preg_match($regex, $str));
    

    Also, note that these expressions are equivalent:

    !(!A || !B)
       A &&  B
    

    So that could be simplified further to just:

    return filter_var($str, FILTER_VALIDATE_URL) && preg_match($regex, $str);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been searching online for awhile, trying to find the best way to
I have been searching around the web all day for the best way to
I have been searching high and low for a way to get my silverlight
I have been searching for a way to insert linebreaks in my code for
I have been told that searching in a MySQL database with LIKE '%wordend' is
I have been searching for a way to skin C# applications under Windows Forms.
Here is the question I have been posed: What is the best way to
For many hours now I have been searching for the perfect way to encrypt
I've been wondering what the best way to store data would be in my
I have been searching for a couple hours for a way to accomplish this

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.