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

The Archive Base Latest Questions

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

I have found a solution for auto detecting links and putting them in the

  • 0

I have found a solution for auto detecting links and putting them in the <a> tag here: Regex PHP – Auto-detect YouTube, image and "regular" links

Relevant part (I had to move the function outside of the preg_replace_callback call for compatibility reasons):

function put_url_in_a($arr)
    {
    if(strpos($arr[0], 'http://') !== 0)
        {
            $arr[0] = 'http://' . $arr[0];
        }
        $url = parse_url($arr[0]);

        //links
        return sprintf('<a href="%1$s">%1$s</a>', $arr[0]);
    }

$s = preg_replace_callback('#(?:https?://\S+)|(?:www.\S+)|(?:\S+\.\S+)#', 'put_url_in_a', $s);

This works fine, except when it stumbles upon a url in an tag, which it then ruins (by putting another tag into it). It also ruins embedded media.

Question: How can I exclude HTML tags from being processed by this function using, hopefully, only regular expressions?

  • 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-28T19:30:01+00:00Added an answer on May 28, 2026 at 7:30 pm

    One option – if the URL is already in a link it must be prefixed by href=', so exclude links with a negative lookbehind assertion:

    #(?<!href\=['"])(?:https?://\S+)|(?:www.\S+)|(?:\S+\.\S+)#
    

    EDIT: — actually the above form won’t work because the URL match is too general, it’ll turn things like ... into a link, incorrectly. Using my own favourite URL matching scheme seems to work correctly:

    $s = preg_replace_callback('#(?<!href\=[\'"])(https?|ftp|file)://[-A-Za-z0-9+&@\#/%()?=~_|$!:,.;]*[-A-Za-z0-9+&@\#/%()=~_|$]#', 'regexp_url_search', $s);
    

    For example: http://codepad.viper-7.com/TukPdY

    $s = "The following link should be linkified: http://www.google.com but not this one: <a href='http://www.google.com'>google</a>."`
    

    Becomes:

    The following link should be linkified: <a href="http://www.google.com">http://www.google.com</a> but not this one: <a href='http://www.google.com'>google</a>.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is something I've pseudo-solved many times and have never quite found a solution
I have searched for hours now and haven't found a solution for my problem.
i have this problem: ms-access could not delete and i found a potential solution
It looks like a problem which could have simple solution, but I haven't found
I have found some libraries or web services in PHP that does the job.
I have found a solution in MSDN for ListView sorting... But I found the
Ok i have found a solution regarding my problem. I have three text boxes
I've searched the Internet and have found some good solutions for teeing STDOUT to
I have looked all around and only found solutions for python 2.6 and earlier,
Most of the ASP.NET MVC paging solutions I have found by googling look like

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.