Making links Clickable

Hi I am having problems with this code. I paid someone to make this and realised that the working links are breaking in a string of text. In the text there are a mix of none url clickable links and some are. I want to make all the url links clickable. Can someone help me fix this please. Also in this script it makes the phone numbers clickable but the only problem in the href part on the link has spaces and it can’t have any spaces in it.
This is wrong
<a href='tel:+44114 234 5679>0114 234 5679</a>
The right way with no spaces
<a href='tel:+441142345679>0114 234 5679</a>

It would be much appreciated for your help and thank you.

function linkify($value, $protocols = array('http', 'mail'), array $attributes = array())
    {
    // Link attributes
    $attr = '';
    foreach ($attributes as $key => $val) {
    $attr .= ' ' . $key . '="' . htmlentities($val) . '"';
    }

    $links = array();

    // Extract existing links and tags
    $value = preg_replace_callback('~(<a .*?>.*?</a>|<.*?>)~i', function ($match) use (&$links) {
    return '<' . array_push($links, $match[1]) . '>';
    }, $value);

    // Extract text links for each protocol
    foreach ((array)$protocols as $protocol) {
    switch ($protocol) {
       			case 'http':
        case 'https':
            $value = preg_replace_callback('~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i', function ($match) use ($protocol, &$links, $attr) {
                if ($match[1]) $protocol = $match[1];
                $link = $match[2] ?: $match[3];
                return '<' . array_push($links, "<a $attr href=\"$protocol://$link\">$link</a>") . '>';
            }, $value);
            break;
        case 'mail':
            $value = preg_replace_callback('~([^\s<]+?@[^\s<]+?\.[^\s<]+)(?<![\.,:])~', function ($match) use (&$links, $attr) {
                return '<' . array_push($links, "<a $attr href=\"mailto:{$match[1]}\">{$match[1]}</a>") . '>';
            }, $value);
            break;
        case 'twitter':
            $value = preg_replace_callback('~(?<!\w)[@#](\w++)~', function ($match) use (&$links, $attr) {
                return '<' . array_push($links, "<a $attr href=\"https://twitter.com/" . ($match[0][0] == '@' ? '' : 'search/%23') . $match[1] . "\">{$match[0]}</a>") . '>';
            }, $value);
            break;
        default:
            $value = preg_replace_callback('~' . preg_quote($protocol, '~') . '://([^\s<]+?)(?<![\.,:])~i', function ($match) use ($protocol, &$links, $attr) {
                return '<' . array_push($links, "<a $attr href=\"$protocol://{$match[1]}\">{$match[1]}</a>") . '>';
            }, $value);
            break;
    }
    }

    // Insert all link
    $url = preg_replace_callback('/<(\d+)>/', function ($match) use (&$links) {
    return $links[$match[1] - 1];
    }, $value);

    $phone = '!(\b\+?[0-9()\[\]./ -]{7,17}\b|\b\+?[0-9()\[\]./ -]{7,17}\s+(extension|x|#|-|code|ext)\s+[0-9]{1,6})!i';
    	
    	
    	
    $result = preg_replace($phone, " <a href='tel:+44$1'>$1</a>", $url);

    //removing 0 and adding +44
    $first = str_replace('+44 0' , '+44', $result);
    return str_replace('+440' , '+44', $first);
    }

Replace the line:

$result = preg_replace($phone, " <a href='tel:+44$1'>$1</a>", $url);

with this:

$result = preg_replace_callback($phone, function ($matches) {
    $number = $matches[0];
    $condensed_number = str_replace(' ', '', $number);
    return " <a href='tel:+44{$condensed_number}'>{$number}</a>";
}, $url);

As an aside; this is code is hella complicated for what it’s doing and looks liable to break. It looks like you’re working on a home made CMS? If you are, I’d recommend having a look at wordpress or similar. You can build a website much more quickly, and it will be easier for your users to create content.

1 Like

Thank you so much, that worked great. just need the fix the url links. The working links are breaking in a string of text. In the text there are a mix of none url clickable links and some are. I want to make all the url links clickable without breaking the working ones.

Sponsor our Newsletter | Privacy Policy | Terms of Service