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 7781087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:06:46+00:00 2026-06-01T19:06:46+00:00

I know there have been many questions asking for help converting URLs to clickable

  • 0

I know there have been many questions asking for help converting URLs to clickable links in strings, but I haven’t found quite what I’m looking for.

I want to be able to match any of the following examples and turn them into clickable links:

http://www.domain.com
https://www.domain.net
http://subdomain.domain.org
www.domain.com/folder
subdomain.domain.net
subdomain.domain.edu/folder/subfolder
domain.net
domain.com/folder

I do not want to match random.stuff.separated.with.periods.

EDIT: Please keep in mind that these URLs need to be found within larger strings of ‘normal’ text. For example, I want to match ‘domain.net’ in “Hello! Come check out domain.net!”.

I think this could be accomplished with a regex that can determine whether the matching url contains .com, .net, .org, or .edu followed by either a forward slash or whitespace. Other than a user typo, I can’t imagine any other case in which a valid URL would have one of those followed by anything else.

I realize there are many valid domain extensions out there, but I don’t need to support them all. I can just choose which to support with something like (com|net|org|edu) in the regex. Unfortunately, I’m not skilled enough with regex yet to know how to properly implement this.

I’m hoping someone can help me find a regular expression (for use with PHP’s preg_replace) that can match URLs based on just about any text connected by one or more dots and either ending with one of the specified extensions followed by whitespace OR containing one of the specified extensions followed by a slash and possibly folders.

I did several searches and so far have not found what I’m looking for. If there already exists a SO post that answers this, I apologize.

Thanks in advance.

— EDIT 3 —

After days of trial and error and some help from SO, here’s what works:

preg_replace_callback('#(\s|^)((https?://)?(\w|-)+(\.(\w+|-)*)+(?<=\.net|org|edu|com|cc|br|jp|dk|gs|de)(\:[0-9]+)?(?:/[^\s]*)?)(?=\s|\b)#is',
                create_function('$m', 'if (!preg_match("#^(https?://)#", $m[2]))
                return $m[1]."<a href=\"http://".$m[2]."\">".$m[2]."</a>"; else return $m[1]."<a href=\"".$m[2]."\">".$m[2]."</a>";'),
                $event_desc);

This is a modified version of anubhava’s code below and so far seems to do exactly what I want. Thanks!

  • 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-06-01T19:06:54+00:00Added an answer on June 1, 2026 at 7:06 pm

    You can use this regex:

    #(\s|^)((?:https?://)?\w+(?:\.\w+)+(?<=\.(net|org|edu|com))(?:/[^\s]*|))(?=\s|\b)#is
    

    Code:

    $arr = array(
    'http://www.domain.com/?foo=bar',
    'http://www.that"sallfolks.com',
    'This is really cool site: https://www.domain.net/ isn\'t it?',
    'http://subdomain.domain.org',
    'www.domain.com/folder',
    'Hello! You can visit vertigofx.com/mysite/rocks for some awesome pictures, or just go to vertigofx.com by itself',
    'subdomain.domain.net',
    'subdomain.domain.edu/folder/subfolder',
    'Hello! Check out my site at domain.net!',
    'welcome.to.computers',
    'Hello.Come visit oursite.com!',
    'foo.bar',
    'domain.com/folder',
    
    );
    foreach($arr as $url) {   
       $link = preg_replace_callback('#(\s|^)((?:https?://)?\w+(?:\.\w+)+(?<=\.(net|org|edu|com))(?:/[^\s]*|))(?=\s|\b)#is',
               create_function('$m', 'if (!preg_match("#^(https?://)#", $m[2]))
                   return $m[1]."<a href=\"http://".$m[2]."\">".$m[2]."</a>"; else return $m[1]."<a href=\"".$m[2]."\">".$m[2]."</a>";'),
               $url);
       echo $link . "\n";
    

    OUTPUT:

    <a href="http://www.domain.com/?foo=bar">http://www.domain.com/?foo=bar</a>
    http://www.that"sallfolks.com
    This is really cool site: <a href="https://www.domain.net">https://www.domain.net</a>/ isn't it?
    <a href="http://subdomain.domain.org">http://subdomain.domain.org</a>
    <a href="http://www.domain.com/folder">www.domain.com/folder</a>
    Hello! You can visit <a href="http://vertigofx.com/mysite/rocks">vertigofx.com/mysite/rocks</a> for some awesome pictures, or just go to <a href="http://vertigofx.com">vertigofx.com</a> by itself
    <a href="http://subdomain.domain.net">subdomain.domain.net</a>
    <a href="http://subdomain.domain.edu/folder/subfolder">subdomain.domain.edu/folder/subfolder</a>
    Hello! Check out my site at <a href="http://domain.net">domain.net</a>!
    welcome.to.computers
    Hello.Come visit <a href="http://oursite.com">oursite.com</a>!
    foo.bar
    <a href="http://domain.com/folder">domain.com/folder</a>
    

    PS: This regex only supports http and https scheme in URL. So eg: if you want to support ftp also then you need to modify the regex a little.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there have been some similar questions to this, but they haven't helped
I know there have been a million questions asking this, but mine is different.
I know there have been a lot of questions similar to my small problem.
I know there have been a few threads on this before, but I have
OK, I know there have already been questions about getting started with TDD ..
I have been looking through the questions asked on StackOverflow, but there are so
I know questions like this have been asked numerous times, but not quite this
I know there have been a lot of questions regarding posting images to a
I have been pouring over many forums and questions similar (but not the same)
I know there are tons of threads regarding this issue but I have not

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.