*

PeterB

  • ***
  • 33 posts
Make links clickable in item description
« on: January 24, 2023, 05:10:20 PM »
I am trying to make the links and email addresses in the description section of an advert clickable. I was able to achieve this a while ago with a much older version of OsClass using the code below.

In the functions.php I added:
Code: [Select]
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
        return preg_replace_callback('/<(d+)>/', function ($match) use (&$links) { return $links[$match[1] - 1]; }, $value);
    }

And then in the item.php

I replaced
Code: [Select]
osc_item_description()
With the following
Code: [Select]
linkify(osc_item_description())
I am not sure why I am picking up errors, as I said this was a piece of code that worked on a much older version. Could someone please help to get it corrected?

Many thanks.
Pete
« Last Edit: February 08, 2023, 07:47:40 PM by MB Themes »

*

MB Themes

Re: How to make links clickable.
« Reply #1 on: January 25, 2023, 11:10:11 AM »
Maybe you want to have default value on protocols to be $protocols = array('http', 'https', 'mail') ?
It's more-less word with regex and probably needs to be debug. I suppose there is many different functions on stackoverflow so try until you find funtional.
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots

Marked as best answer by frosticek on February 08, 2023, 07:46:43 PM
*

PeterB

  • ***
  • 33 posts
Re: How to make links clickable.
« Reply #2 on: February 07, 2023, 07:50:45 PM »
Sorry I know that this is an old post but I just thought I would post the script I finally used:

Code: [Select]
function linkify($text) {
    $url_pattern = '/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/';
    $email_pattern = '/\S+@\S+\.\S+/';

    $text = preg_replace($url_pattern, '<a href="$0">$0</a>', $text);
    $text = preg_replace($email_pattern, '<a href="mailto:$0">$0</a>', $text);

    return $text;
}

Or use

Code: [Select]
function linkify($text) {
    $url_pattern = '/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/';
    $email_pattern = '/\S+@\S+\.\S+/';

    $text = preg_replace($url_pattern, '<a href="$0" target="_blank">$0</a>', $text);
    $text = preg_replace($email_pattern, '<a href="mailto:$0">$0</a>', $text);

    return $text;
}

If you want to open links in a new tab.
Very simple
« Last Edit: February 07, 2023, 07:56:29 PM by PeterB »

*

MB Themes

Re: How to make links clickable.
« Reply #3 on: February 08, 2023, 07:46:54 PM »
Thanks for sharing!
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots