How to Auto Convert plain text URLs into HTML hyperlinks in PHP

Usage:

$string ='Test https://google.com';
$post   = make_clickable($string);
echo $post;

PHP Function:

function _make_url_clickable_cb($matches)
{
    $ret = '';
    $url = $matches[2];
    if (empty($url))
        return $matches[0];
    if (in_array(substr($url, -1), array(
        '.',
        ',',
        ';',
        ':'
    )) === true) {
        $ret = substr($url, -1);
        $url = substr($url, 0, strlen($url) - 1);
    }
    return $matches[1] . "<a href=\"$url\" rel=\"nofollow\" target=\"_blank\">$url</a>" . $ret;
}
function _make_web_ftp_clickable_cb($matches)
{
    $ret  = '';
    $dest = $matches[2];
    $dest = 'http://' . $dest;
    if (empty($dest))
        return $matches[0];
    if (in_array(substr($dest, -1), array(
        '.',
        ',',
        ';',
        ':'
    )) === true) {
        $ret  = substr($dest, -1);
        $dest = substr($dest, 0, strlen($dest) - 1);
    }
    return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>" . $ret;
}
function _make_email_clickable_cb($matches)
{
    $email = $matches[2] . '@' . $matches[3];
    return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
}
function make_clickable($ret)
{
    $ret = ' ' . $ret;
    $ret = preg_replace_callback('#([\s>])([\w]+?://[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_url_clickable_cb', $ret);
    $ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_web_ftp_clickable_cb', $ret);
    $ret = preg_replace_callback('#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret);
    $ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret);
    $ret = trim($ret);
    return $ret;
}

 

How to choose a random word from an array php
<?php $Words =array("Black","Yellow","Blue","Red","Purple","Pink", "White"); $rand_word=array_rand($Words,2); $Result =
How to minify HTML code of PHP page
<?php ob_start("minifier"); function minifier($code) { $search =
How to copy a file from one directory to another using PHP
<?php // Copy the file from /user/desktop/geek.txt
How to Deleting all files from a folder using PHP
<?php // PHP program to delete all
How do I test if an remote image file exists in php
if(@get_headers($your_url) == 'HTTP/1.1 404 Not Found') {
Human Readable File Size with PHP
function bytesToHuman($bytes) { $units = ['B', 'KB',
PHP Simple File Upload from Remote URL
<?php if($_POST){ //get the url $url =
PHP unzipper / extracts .zip archives and .gz files on webservers
<?php $timestart = microtime(TRUE); $arc = new
Get the first character in a string with PHP
//Example string. $string = "My milkshake brings
How to generate a random token with PHP
//Generate a random string. $token = openssl_random_pseudo_bytes(16);
How to handle HTML checkboxes with PHP
<?php //Set "terms accepted" to false by
Generating random names with PHP
<?php //PHP array containing forenames. $names =
How to create PHP Timer Countdown
//You must call the function session_start() before
PHP – Do not echo out HTML!
Wrong: <?php echo '<div id="user_profile">'; echo '<h1>'
How to rotate a series of words on page load using PHP
$words = array('fox', 'hippo', 'dog', 'cat'); echo
How to check if a value exists in an array in PHP
$zoo = array("Lion", "Elephant", "Tiger", "Zebra", "Rhino",
How to Get the Current Date and Time in PHP
date_default_timezone_set('Asia/Kolkata'); $date = date('d-m-y h:i:s'); echo $date;
How to remove HTML special characters from a string in PHP
$my_str = "String with <b>special</b> characters."; //
How to extract substring from a string in PHP
$str = "Hello World!"; echo substr($str, 0,

Top