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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:47:03+00:00 2026-05-17T21:47:03+00:00

When a user enters a URL, e.g. http://www.google.com , I would like to be

  • 0

When a user enters a URL, e.g. http://www.google.com, I would like to be able to parse that text using PHP, find any links, and replace them with <a> tags that include the original URL as an HREF.

In other words, http://www.google.com will become

<a href="http://www.google.com">http://www.google.com</a>

I’d like to be able to do this for all URLs of these forms (with .com interchangeable with any TLD):

http://www.google.com
www.google.com
google.com
docs.google.com

What’s the most performant way to do this? I could try writing some really fancy regex, but I doubt that’s the best method available to me.

For bonus points, I’d also like to prepend http:// to any URL lacking it, and strip the display text itself down to something of the form http://www.google.com/reallyLongL... and display an external link icon afterwards.

  • 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-17T21:47:04+00:00Added an answer on May 17, 2026 at 9:47 pm

    I got this working exactly the way I want here:

    <?php
    
    $input = <<<EOF
    http://www.example.com/
    http://example.com
    www.example.com
    http://iamanextremely.com/long/link/so/I/will/be/trimmed/down/a/bit/so/i/dont/mess
    /up/text/wrapping.html
    EOF;
    
      function trimlong($match)
      {
        $url = $match[0];
        $display = $url;
        if ( strlen($display) > 30 ) {
          $display = substr($display,0,30)."...";
        }
        return '<a href="'.$url.'">'.$display.' <img src="http://static.goalscdn.com/img/external-link.gif" height="10" width="11" /></a>';
      }
    
    $output = preg_replace_callback('#(http://|www\\.)[^\\s<]+[^\\s<,.]#i',
                                     array($this,'trimlong'),$input);
    
    echo $output;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following url structure: http://localhost/sitename/views/www/page.php I would like for page.php to show
I would like to intercept any URL which the user enters in their browser
I need a user to be able to enter a URL, and would like
In my program a user enters a url string, say http://www.engineering.uiowa.edu/~hawkeng//fall01/graphics/potato.gif how would I
I am creating a Java application which performs URL search for site: http://www.alexa.com/topsites/category User
I have a url on my joomla site http://www.mysite.com/index.php?option=com_content&view=frontpage&Itemid=3220 My Customer wants it to
I have used this: Generate Google Analytics events (__utm.gif requests) serverside and this: http://www.garyrgilbert.com/blog/index.cfm/2008/10/21/Tracking-Digital-Content
when url= http://www.iranfairco.com/MainPP.aspx?idCompany=142707090021 . change content MainPP.aspx by idCompany for example idCompany=142707090021 equal companyName
When a user enters a bookmark using form['f3'] (a url and a title) it
I am using this regular expression to validate youtube urls. ^http:\/\/(?:www\.)?youtube.com\/watch\?(?=.*v=\w+)(?:\S+)?$ it works great.

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.