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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:22:15+00:00 2026-06-05T02:22:15+00:00

I am using a PHP function to automatically turn URLs in a text string

  • 0

I am using a PHP function to automatically turn URLs in a text string into an actual link that people can click on. It seems to work in most cases, however I have found some cases where it does not.

I don’t really understand regular expressions at all, so I was hoping someone could help me out with this.

Here is the pattern I’m currently using:

$pattern = "/(((http[s]?:\/\/)|(www\.))(([a-z][-a-z0-9]+\.)?[a-z][-a-z0-9]+\.[a-z]+(\.[a-z]{2,2})?)\/?[a-z0-9.,_\/~#&=;%+?-]+[a-z0-9\/#=?]{1,1})/is";

However here are some links I have found that this pattern is not matching:

  • http://www.oakvilletransit.ca – Not sure, but assuming it doesn’t match because of the two-letter country code
  • http://www.grt.ca – Another one with the .ca domain that is not working
  • Several other .ca addresses
  • freepublictransports.com – Addresses without www. or http:// in front of them. I would like these to work as well.
  • http://www.222tips.com – Assuming it doesn’t match because of the numbers at the beginning of the address.

Does anyone know how I can modify that regex pattern to match these cases as well?

EDIT – It should also match URLs that may have a period at the end. If a URL is the last part of a sentence there may be a period at the end that should not be included in the actual link. Currently this pattern takes that into account as well.

EDIT 2 – I am using the pattern like this:

$pattern = "/((http|https):\/\/)?([a-z0-9-]+\.)?[a-z][a-z0-9-]+(\.[a-z]{2,6}){1,3}(\/[a-z0-9.,_\/~#&=;%+?-]*)?/is";
  $string = preg_replace($pattern, " <a target='_blank' href='$1'>$1</a>", $string);
  // fix URLs without protocols
  $string = preg_replace("/href='www/", "href='http://www", $string);
  return $string;
  • 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-05T02:22:16+00:00Added an answer on June 5, 2026 at 2:22 am

    The following regex will match URLs:

    • (Optionally) With http:// or https://
    • (Optionally) With a subdomain (www.example.com, help.example.com, etc)
    • With 1-3 domain extensions, which each must be 2-6 characters (www.example.com.gu, www.example.com.au.museum, etc)
    • (Optionally) With a forward slash at the end
    • (Optionally) With valid characters after the forward slash

    The /i at the end makes it case insensitive.

    /((http|https):\/\/)?([a-z0-9-]+\.)?[a-z0-9-]+(\.[a-z]{2,6}){1,3}(\/[a-z0-9.,_\/~#&=;%+?-]*)?/is

    Edit: This will not match any “hanging” periods at the end (such as the end of a sentence) because it’s not part of the URL, and shouldn’t be included in the href attribute of your link.

    Edit 2: In your first preg_replace(), change $1 to $0. This will insert the entire matched string instead of a single part of it.

    Edit 3: (Update 2) Here’s a better way you can check for a http:// or https:// at the beginning:

    preg_replace("/href='[^h][^t][^t][^p][^s]?[^:]/", "/href='http:\/\/", $string);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP function that takes a variable number of arguments (using func_num_args()
I am using ack to search for instances of the PHP function split( that
How can i get all the users from the LDAP using PHP. function getUsers()
How can I automatically check errors in my code when I'm using Sublime Text
I'm preparing a function in PHP to automatically convert a string to be used
I am using php sendmail() function in my projects. When I sent above 3
I'm using PHP's function file_get_contents() to fetch contents of a URL and then I
I actually want to generate random password using PHP uniqid() function $randomPassword = uniqid();
i am using php and mail function , but i face this error: Warning:
I am calling a php function using ajax call but its not working. I

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.