Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,205,352 members, 7,992,108 topics. Date: Saturday, 02 November 2024 at 04:31 PM

Convert URL To Clickable Link Using PHP - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Convert URL To Clickable Link Using PHP (853 Views)

Urgent |how Do I Create A Google Drive Image Clickable And Redirected To Url? / HELP!!! Run Button Is Not Clickable On Android Studio / Create Clickable Comment Link In PHP (2) (3) (4)

(1) (Reply)

Convert URL To Clickable Link Using PHP by skptricks: 12:16pm On Jan 07, 2018
This post explains how to Convert URL To Clickable Link Using PHP. Here I am using below PHP function to turn all URLs in clickable links with the help of Regular Expression validation.

Post link : http://www.skptricks.com/2018/01/convert-url-to-clickable-link-using-php.html

lets see the user defined PHP Function which convert URL to Clickable Link.

convertLink.php
This function covert URL to Link with the help of Regular Expression validation.

<?php
function Convert_link_to_urls($text = '')
{
$text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text);
$finaltext = ' ' . $text;
$finaltext = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<span class='ccc'><a href=\"\\2\" target=\"_blank\"><font style='font-family: Verdana, Geneva, sans-serif;color: blue;font-size:13px; line-height:20px;'>\\2</font></a></span>", $finaltext);
$finaltext = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<span class='ccc'><a href=\"http://\\2\" target=\"_blank\"><font style='font-family: Verdana, Geneva, sans-serif;color: blue;font-size:13px; line-height:20px;'>\\2</font></a></span>", $finaltext);
$finaltext = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<span class='ccc'><a href=\"mailto:\\2@\\3\"><font style='font-family: Verdana, Geneva, sans-serif;color: blue;font-size:13px; line-height:20px;'>\\2@\\3</font></a></span>", $finaltext);
$finaltext = substr($finaltext, 1);
return $finaltext;
}
?>

Index.php
Here we are calling Convert_link_to_urls Function to display the clickable Link:

<?php
include('convertLink.php');

$text='welcome to skptricks : http://www.skptricks.com/2017/12/download-urls-content-using-php-curl.html';

echo Convert_link_to_urls($text);

?>

Post link : http://www.skptricks.com/2018/01/convert-url-to-clickable-link-using-php.html

1 Like

(1) (Reply)

Betting Site / How To Remove Category From URL Permalink / It's #8days To Tribe Nigeria's Software Accelerator

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 9
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.