PSD to HTML conversion PSD to HTML conversion PSD2HTML.com with over 300 professionals takes the designs to HTML and beyond

Code Snippet

Home » Code Snippets » PHP » Find URLs in Text, Make Links

Find URLs in Text, Make Links

<?php

// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

// The Text you want to filter for urls
$text = "The text you want to filter goes here. http://google.com";

// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {

       // make the urls hyper links
       echo preg_replace($reg_exUrl, "<a href="{$url[0]}">{$url[0]}</a> ", $text);

} else {

       // if no urls in the text just return the text
       echo $text;

}
?>

The basic function of this is to find any URLs in the block of text and turn them into hyperlinks. It will only find URLs if they are properly formatted, meaning they have a http, https, ftp or ftps.

Check out the comments below for more solutions.

Subscribe to The Thread

  1. I want to thank you from the bottom of my little heart, I have been in search for this exact script for the past 6 months! Thank you, thank you, thank you. I would like to point out however that there was error but nothing I could not fix :D Assid from your code this is the code that I got to work for myself.

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

    Again thank you very much!

  2. This doesn’t work for 2 or more URLs.

  3. Find the Image URL using preg_match() syntax or something similar to extract JPG or PNG or GIF URLs from a mixed text and put them in an array or at last store the first url.

    maybe some syntax which searchs for strings that are beginning with http and ending with jpg/png/gif..

    i believe it can be done with preg_match()

    Note: the text can be like that : $string =blablablabla”http://www.xxx.com/xxx.jpg”blablablabla

    $matches = array();
    preg_match_all(‘!http://.+\.(?:jpe?g|png|gif)!Ui’ , $string , $matches);

  4. Thanks,

    It works like charm!

  5. Wolandca
    public function formatUrlsInText($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();
                foreach($matches[0] as $pattern){
                    if(!array_key_exists($pattern, $usedPatterns)){
                        $usedPatterns[$pattern]=true;
                        $text = str_replace  ($pattern, "<a href="{$pattern}" rel="nofollow">{$pattern}</a> ", $text);
                    }
                }
                return $text;
    }
  6. This is my version based on previous examples… hope this helps someone trying to force links on www without the http…

    simply call the function as follows:

    $body = txt2link($body);

    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);
    }
  7. Cases that this won’t catch:
    http://localhost/test
    http://1.2.3.4/test

    Also if you feed in
    “http://www.google.co.uk/page is on the website http://www.google.co.uk/
    you will get some very mangled output, as when you search for “http://www.google.co.uk/” you will also match the text in the middle of the existing link for “http://www.google.co.uk/page”

  8. your site is amazing. no bull shit. all good stuff. pls include my email id in ur permanent mailing list.

  9. can that original script for finding urls be modifed to look for links ending with an .mp3 exstension?

  10. Thanks, working a charm!

  11. Great post. Just what I was looking for. I’m going to use it on my site.

    Thanks.

  12. Another equivalent function that make better results : http://code.seebz.net/p/autolink-php/

    There is the same in javascript : http://code.seebz.net/p/autolink-js/

  13. The domain name can be longer then 3 characters, e.g. http://cxid.info/

  14. Great trick for developing the regular expression.

  15. I’ve changed it a little bit, cause it won’t work for more than one url. Besides THANK YOU very much for the help.

    public static function url_to_link($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_all($reg_exUrl, $text, $url)) {
                    // make the urls hyper links
                    foreach($url[0] as $v){
                        //current position of the searached url
                        $curpos = strpos($text,' '.$v)+1;
                        //delete the url
                        $text = substr_replace($text,'', $curpos, strlen($v));
                        //insert the link
                        $text = substr_replace($text,''.$v.'', $curpos ,0);
                    }
                    return $text;
                }
                else {
                    // if no urls in the text just return the text
                    return $text;
                }
            }
  16. i really liked this preg :D

  17. You Have some useful ideas! Maybe I ought to ponder trying this by my self. Cheers

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~