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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:03:17+00:00 2026-06-02T00:03:17+00:00

I would like to detect if a URI is located in a string, properly

  • 0

I would like to detect if a URI is located in a string, properly sanitize it, and output it with the proper anchor tags.

ie, a user inputs:

Check out our profile on facebook!
https://facebook.com/ourprofile

and our twitter!
twitter.com/#!/ourprofile

and email us!
ourprofile@stack.com

Is there a way to determine that there are URIs located in the string, sanitize unsafe characters and properly output a safe anchor?

so the output would be:

Check out our profile on facebook!
<a href="https://www.facebook.com/ourprofile">https://www.facebook.com/ourprofile</a>

and our twitter!
<a href="http://www.twitter.com/#!/ourprofile">twitter.com/#!/ourprofile</a>

and email us!
<a href="mailto:ourprofile@stack.com">ourprofile@stack.com</a>

the ideas i had in mind were using preg_match and simple preg_replace for removing unsafe characters, but this has failed me and im just going in circles, i dont really know where to start for this, as im almost certain a blacklist approach such as that is not proper or secure.

  • 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-02T00:03:19+00:00Added an answer on June 2, 2026 at 12:03 am

    I found this at experts-exchange.com. Hope, it helps:

    function make_links($text)
    {
      return  preg_replace(
         array(
           '/(?(?=<a[^>]*>.+<\/a>)
                 (?:<a[^>]*>.+<\/a>)
                 |
                 ([^="\']?)((?:https?|ftp|bf2|):\/\/[^<> \n\r]+)
             )/iex',
           '/<a([^>]*)target="?[^"\']+"?/i',
           '/<a([^>]+)>/i',
           '/(^|\s)(www.[^<> \n\r]+)/iex',
           '/(([_A-Za-z0-9-]+)(\\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-]+)
           (\\.[A-Za-z0-9-]+)*)/iex'
           ),
         array(
           "stripslashes((strlen('\\2')>0?'\\1<a href=\"\\2\">\\2</a>\\3':'\\0'))",
           '<a\\1',
           '<a\\1 target="_blank">',
           "stripslashes((strlen('\\2')>0?'\\1<a href=\"http://\\2\">\\2</a>\\3':'\\0'))",
           "stripslashes((strlen('\\2')>0?'<a href=\"mailto:\\0\">\\0</a>':'\\0'))"
           ),
           $text
       );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UIImagePickerController subclass, and I would like to detect when the user
I would like to detect whether the user has pressed Enter using jQuery. How
I would like to detect (via c++) what the user has chosen for text
I am using phonegap for build android apps. I would like to detect user's
In an Android app I'm making, I would like to detect when a user
I would like to detect when user touches screen constantly for 5 seconds. No
I would like to detect if a user has cookies turned off and redirect
I would like to detect dropdown lists on my page that the user didn't
I would like to detect underflow when converting string to double in C++ (Visual
I would like to detect whether the OS I'm compiling on is Windows. Is

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.