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

  • Home
  • SEARCH
  • 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 7398651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:53:48+00:00 2026-05-29T03:53:48+00:00

Well this is the function which active the link if someone put a link

  • 0

Well this is the function which active the link if someone put a link in message box with text.

My question is it doesn’t show more than 1 link if someone put many link ex: http://www.yahoo.com http://www.gmail.com http://www.facebook.com, Then it’s show only first link http://www.yahoo.com

function txt2link($text){
    // force http: on www.
    $text = ereg_replace( "www\.", "http://www.", $text );
    // eliminate duplicates after force
    $text = ereg_replace( "http://http://www\.", "http://www.", $text );
    $text = ereg_replace( "https://http://www\.", "https://www.", $text );

    // The Regular Expression filter
    $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    // Check if there is a url in the text
    if(preg_match($reg_exUrl, $text, $url)) {
        // make the urls hyper links
        $text = preg_replace($reg_exUrl, '<a href="'.$url[0].'" rel="nofollow">'.$url[0].'</a>', $text);
    }    // if no urls in the text just return the text
    return ($text);
}

$url = "Alter pot waer it your pot http://css-tricks.com/snippets/php/find-urls-in-   
        text-make-links/  you may click the link www.yahoo.com or you may see what is the 
        http://www.youtube.com say, is it right?";

echo txt2link($url);

you can run this code to see the result.

Any Idea ?

  • 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-29T03:53:49+00:00Added an answer on May 29, 2026 at 3:53 am

    This is one that someone here helped me build up a while back, I dunno where it is on stack anymore but I still use this function to date.. This handles many urls in one shot, with or without http: with or without www. in most cases, its true this could us a bit of refining, but in all does the job really nice.

    //for finding URLs within  body of text and converting them to a clickable link
    //checks DNS to see if its valid and if the link HTML already exists ignores it.
    function titleHyper($text){
                    $text = preg_replace( "/(www\.)/is", "http://", $text);
                    $text = str_replace(array("http://http://","http://https://"), "http://", $text);
                    $text = str_replace(array("<a href='", "<a href=\"", "</a>", "'>", "\">"), "", $text);
                    $reg_exUrl = "/(http|https|ftp|ftps|)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
                    preg_match_all($reg_exUrl, $text, $matches);
                    $usedPatterns = array();
                    $context = stream_context_create(array(
                    'http' => array(
                    'timeout' => 5
                    )
                    ));
                    foreach($matches[0] as $pattern){
                         if(!array_key_exists($pattern, $usedPatterns)){
                              $usedPatterns[$pattern]=true;
                              $the_contents = @file_get_contents($pattern, 0, $context);
                              if(substr(trim($pattern), 0, 8) != "https://"){
                              $color = "#FF0000";
                              }
                              if (empty($the_contents)) {
                              $title = $pattern;
                              } else {
                              preg_match("/<title>(.*)<\/title>/Umis", $the_contents, $title);
                              $title = $title[1];
                              $color = "#00FF00";                    
                              //$title = htmlspecialchars($title, ENT_QUOTES); //saving data to database
                              }                    
                              $text = str_ireplace($pattern, "<a style='font-size: 14px; background-color: #FFFFFF; color: $color;' href='$pattern' rel='nofollow' TARGET='_blank'> $title </a>", $text);
    
                         }
                    }
                    return $text;
    }
    // titleHyper() in action example:
    //$text = "Some sample text with WWW.AOL.com<br />http://www.youtube.com/watch?v=YaxKiZfQcX8 <br />Anyone use www.myspace.com?  <br />Some people are nuts, look at this stargate link at http://www.youtube.com/watch?v=ZKoUm6z5SzU&feature=grec_index , like aliens exist or something. http://www.youtube.com/watch?v=sfN-7HczmOU&feature=grec_index  and here's a secure site https://familyhistory.hhs.gov, unless you use curl or allow secure connections it will never get a title. <br /> This is a not valid site http://zzzzzzz and this is a dead site http://zwzwzwxzw.com.<br /> Lastly lets try an already made hyperlink and see what it does <a href='http://tacobell.com'>taco bell</a>";
    //echo titleHyper($text);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first experience with javascript, and... Well... Ugh. Here's what's happening: function
Well this file was put in the repo by mistake and was deleted and
I wonder what this means in F#. a function taking an integer, which returns
Well this is incredibly frustrating. After being nagged by Rails that I need to
Well this is a really weird issue, I really didn't find anything on this
I hope I can explain this well enough so that you can understand the
I have previously read Spolsky's article on character-encoding, as well as this from dive
Sorry for the unhelpful title, but hopefully I can explain this well enough. Lets
well i have this messages table with sample values like these: msg_id recipient_id read
Well I have this MySQL stored procedure that I wrote and if I run

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.