How to Detect a URL / Hyperlink within text - using PHP
By: irshido | Jul 18, 2011 | In: How To | Likes: 2 | Views:2426

PHP comes with numerous built in functions that come handy in many instances. 

In the below example, we are making use of the preg_match() and preg_replace() funtions that come built in with PHP, to detect and display URL's or Hyperlinks within text. Detecting URL's within text & displaying them is one of the essential things any web developer should be aware of. 

Lets take the below paragraph for example:

"The aim of http://www.TechDesk.info is to help everyone learn the latest trends in IT and help them familiarize themselves.."

as you can see there is a a url on the 1st line but it is displayed as normal text. The intention of this tutorial is to detect that url from the text and display it as a hyperlink using PHP

Example 01:

Checking for URL's on text Saved in a Variable

 

// define a variable that will hold characters of a URL

$hyperlink = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4}(\/\S*)?/";  

//Store the text containing the URL's in the variable '$para' 

$para ="The aim of http://www.TechDesk.info is to help everyone learn the latest trends in IT and help them familiarize themselves..";

//Variable that holds the text that contains the undetected URL

$para = preg_replace($hyperlink, '<a href="'.$url[0].'" rel="nofollow">'.$url[0].'', $para); 

//Check if there is a url in the post using the PHP preg_match function

if (preg_match($hyperlink, $para, $url)) {

// If a URL match is found, display it as a URL

$para = preg_replace($hyperlink, ''.$url[0].'', $para); 

}

// Display the results 

echo "$para" ;

?>

The result for the above code will be 
The aim of http://www.TechDesk.info is to help everyone learn the latest trends in IT and help them familiarize themselves.
 

Example 02:

Checking for URL's from results retrieved from a MySQL database

 <?php

//define an empty variable 

$para ="";

// define a variable that will hold characters of a URL

$hyperlink = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4}(\/\S*)?/";  

// Connect to the database

$db_host = "localhost";

$db_username = "user01";

$db_pass = "password123";

$db_name = "test_db";

@mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");

@mysql_select_db("$db_name") or die ("Database connection failure! ");

// after connecting to the database

//select the row containing the text from the table ‘test_tbl’

$sql = mysql_query("SELECT paragraph  FROM test_tbl "); /

while($row = mysql_fetch_array($sql)){

      // save the result in a variable

       $para = $row["paragraph "];

          // Check if there is a url in the post

         if (preg_match($hyperlink, $para, $url)) {

         // make the urls found within the text as hyper links

         $para = preg_replace($hyperlink, '<a href="'.$url[0].'" rel="nofollow">'.$url[0].'</a>', $para);

          }

}

// Display the results 

echo "$para" ;

?>

 

The result for the above code will be 
The aim of http://www.TechDesk.info is to help everyone learn the latest trends in IT and help them familiarize themselves.

Hope the above explanation would have helped you.
Dont hesitate to ask any questions  or address your input in the comments field below..

 

 

 


You may also Like :
A Good way to clean your computer
   What Do you Think of this Post?
Like This Post
2 people Like this
Log In or Register to Respond to this post