Solved

make url clickable

Posted on 2014-07-17
19
326 Views
Last Modified: 2014-07-17
The function I found below works perfect for cases like "https://secure.experts...."
but it does not support the links without "http"  like www.experts-exchange.com

Can anybody modify it  to work also without "http"?

Thank you


function make_clickable($ret) {
      
      $ret = ' ' . $ret;
      // in testing, using arrays here was found to be faster
      $ret = preg_replace_callback('#([\s>])([\w]+?://[\w\\x80-\\xff\#!$%&~/.\-;:=,?@\[\]+]*)#is', '_make_url_clickable_cb', $ret);
      

      $ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret);

      
      $ret = trim($ret);
            
      return $ret;
      
}

function _make_url_clickable_cb($matches) {
      $ret = '';
      $url = $matches[2];
 
      if ( empty($url) )
            return $matches[0];
      // removed trailing [.,;:] from URL
      if ( in_array(substr($url, -1), array('.', ',', ';', ':')) === true ) {
            $ret = substr($url, -1);
            $url = substr($url, 0, strlen($url)-1);
            
      }
      
      if (strlen($url)>50) $urls=substr($url,0,50).'...';
      else  $urls=$url;
      
      return $matches[1] . "<a  href=\"$url\" target=\"_blank\" >$urls</a>" . $ret;
      
      

}
0
Comment
Question by:myyis
  • 8
  • 7
  • 4
19 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Teaching example here: http://iconoun.com/demo/extract_clickable_links.php

<?php // extract_clickable_links.php
error_reporting(E_ALL);

// CONVERT A TEXT STRING SO THAT URL REFERENCES ARE MADE INTO CLICKABLE LINKS

function make_clickable_links($text)
{
    $text = str_replace('&amp;', '&', $text);
    $text = preg_replace('#([^"])(((f|ht){1}tps?://)[\-A-Z0-9&@:%_\+\.~\#\?&//=]+)#i', '\\1<a class="offsite" target="_blank" href="\\2">\\2</a>',        $text);
    $text = preg_replace('#([^/])(www.[\-A-Z0-9&@:%_\+.~\#\?&//=]+)#i',                '\\1<a class="offsite" target="_blank" href="http://\\2">\\2</a>', $text);
    $text = preg_replace('#([_\.0-9A-Z\-]+@([0-9A-Z][0-9A-Z\-]+\.)+[A-Z]{2,6})#i',     '<a class="offsite mailto" href="mailto:\\1">\\1</a>',             $text);
    return $text;
}

function extract_clickable_links($text)
{
    preg_match_all('#(\<a .*?\</a\>)#i', $text, $match);
    return $match[0];
}

// SOME TEST DATA
$txt = <<<EOTXT
This is a test of the www.IcoNoun.com/index.php Web Site.
The site can also be addressed like this: http://iconoun.com using the protocol.  Email is like Ray.Paseur@gmail.com.
Multiple lines are OK but the URL must be on one like, like https://paypal.com for this to work.
It can find ftp://facebook.com or https://facebook.com.
It will not find something like foo.com or test.twitter.com.
EOTXT;

// USE THE METHOD
$new = make_clickable_links($txt);

// SHOW THE ORIGINAL AND THE WORK PRODUCT
echo $txt . '<br/>' . PHP_EOL;
echo $new . '<br/>' . PHP_EOL;

echo "<pre>";
$arr = extract_clickable_links($new);
foreach ($arr as $lnk)
{
    echo PHP_EOL;
    echo htmlentities($lnk);
}

Open in new window

0
 

Author Comment

by:myyis
Comment Utility
Ray  thank you  for your answer. I see that  the function you've sent does not support a link like this "http://www.vistaprint.com/local-search-profile.aspx?txi=15664&xnid=TopNav_Local+Search+Profile+%28linked+item%29_Websites&xnav=TopNav"
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
<?php
$str="Link type 1 - www.google.com <br>link type 2 - http://www.google.com <br>link type 3 - https://www.google.com <br>link type 4 - http://google.com <br>Link type 5 - http://www.vistaprint.com/local-search-profile.aspx?txi=15664&xnid=TopNav_Local+Search+Profile+%28linked+item%29_Websites&xnav=TopNav";

$pattern = '#(www\.|https?://)?[\w\d]+\.[\w\d]{2}\S*#i';
$str1=preg_replace($pattern, '<a href="$0">$0</a>', $str);
$str1=str_replace('href="www','href="http://www',$str1);
echo $str1;

Open in new window

0
 

Author Comment

by:myyis
Comment Utility
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Works for me.
0
 

Author Comment

by:myyis
Comment Utility
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Sample with link

http://3v4l.org/rHhiq
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
http://3v4l.org/JvBOW

Previous one had an erroneous carriage return.
0
 

Author Comment

by:myyis
Comment Utility
Sorry my bad, It works thank you. The original function cuts the link like this, can we apply this  also?


 if (strlen($url)>50) $urls=substr($url,0,50).'...';
      else  $urls=$url;
      return $matches[1] . "<a  href=\"$url\" target=\"_blank\" >$urls</a>" . $ret;
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 58

Expert Comment

by:Gary
Comment Utility
$str="Link type 1 - www.google.com <br>Link type 6 - http://drive.google.com/?tab=mo&authuser=0#folders/0B7ZWeOAbtjQ1OHR1Yf343f33N2M <br>link type 2 - http://www.goe3ogLEe.com <br>link type 3 - https://www.google.com <br>link type 4 - http://google.com <br>Link type 5 - http://www.vistaprint.com/local-search-profile.aspx?txi=15664&xnid=TopNav_Local+Search+Profile+%28linked+item%29_Websites&xnav=TopNav <br>";

$pattern = '#(www\.|https?://)?[\w\d]+\.[\d\w]{2}\S*#i';

$str1 = preg_replace_callback(
	$pattern,
	function($matches) {
		strlen($matches[0])>50?$url=substr($matches[0],0,50)."...":$url=$matches[0];
		return '<a href="'.$matches[0].'">'.$url.'</a>';
	},
	$str);

echo $str1;

Open in new window

0
 

Author Comment

by:myyis
Comment Utility
I got this error for the below?

Parse error: syntax error, unexpected T_FUNCTION in /public_html/functions.php on line 1248

echo make_clickable_new('www.weggle.com') ;

function make_clickable_new($str) {
      
$pattern = '#(www\.|https?://)?[\w\d]+\.[\d\w]{2}\S*#i';

$str1 = preg_replace_callback(
      $pattern,
      function($matches) {
            strlen($matches[0])>50?$url=substr($matches[0],0,50)."...":$url=$matches[0];
            return '<a href="'.$matches[0].'">'.$url.'</a>';
      },
      $str);

return $str1;
}
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
What is line 1248?
Works fine for me. Though I forgot to put back in the replacement for just www so the new function is

function make_clickable_new($str) {
       
 $pattern = '#(www\.|https?://)?[\w\d]+\.[\d\w]{2}\S*#i';

 $str1 = preg_replace_callback(
       $pattern,
       function($matches) {
             strlen($matches[0])>50?$url=substr($matches[0],0,50)."...":$url=$matches[0];
             return str_replace('href="www','href="http://www','<a href="'.$matches[0].'">'.$url.'</a>');
       },
       $str);

 return $str1;
 } 

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
Comment Utility
I do not see any problem.  
http://iconoun.com/demo/extract_clickable_links.php

Here is the original snippet modified to add the links you said did not work.  If you have other test cases, it would be helpful for you to post them instead of making us guess at them!  If you do that, then we can use the principles of Test Driven Development ("TDD") to save you a lot of time and get you the answers you want.

<?php // extract_clickable_links.php
error_reporting(E_ALL);

// CONVERT A TEXT STRING SO THAT URL REFERENCES ARE MADE INTO CLICKABLE LINKS

function make_clickable_links($text)
{
    $text = str_replace('&amp;', '&', $text);
    $text = preg_replace('#([^"])(((f|ht){1}tps?://)[\-A-Z0-9&@:%_\+\.~\#\?&//=]+)#i', '\\1<a class="offsite" target="_blank" href="\\2">\\2</a>',        $text);
    $text = preg_replace('#([^/])(www.[\-A-Z0-9&@:%_\+.~\#\?&//=]+)#i',                '\\1<a class="offsite" target="_blank" href="http://\\2">\\2</a>', $text);
    $text = preg_replace('#([_\.0-9A-Z\-]+@([0-9A-Z][0-9A-Z\-]+\.)+[A-Z]{2,6})#i',     '<a class="offsite mailto" href="mailto:\\1">\\1</a>',             $text);
    return $text;
}

function extract_clickable_links($text)
{
    preg_match_all('#(\<a .*?\</a\>)#i', $text, $match);
    return $match[0];
}

// SOME TEST DATA
$txt = <<<EOTXT
This is a test of the www.IcoNoun.com/index.php Web Site.
The site can also be addressed like this: http://iconoun.com using the protocol.  Email is like Ray.Paseur@gmail.com.
Multiple lines are OK but the URL must be on one like, like https://paypal.com for this to work.
It can find ftp://facebook.com or https://facebook.com.
It will not find something like foo.com or test.twitter.com.
Here is http://www.vistaprint.com/local-search-profile.aspx?txi=15664&xnid=TopNav_Local+Search+Profile+%28linked+item%29_Websites&xnav=TopNav in text
Here is https://drive.google.com/?tab=mo&authuser=0#folders/0B7ZWeOAbtjQ1OHR1Yf343f33N2M

EOTXT;

// USE THE METHOD
$new = make_clickable_links($txt);

// SHOW THE ORIGINAL AND THE WORK PRODUCT
echo $txt . '<br/>' . PHP_EOL;
echo $new . '<br/>' . PHP_EOL;

echo "<pre>";
$arr = extract_clickable_links($new);
foreach ($arr as $lnk)
{
    echo PHP_EOL;
    echo htmlentities($lnk);
}

Open in new window

0
 

Author Comment

by:myyis
Comment Utility
ok it is working.
The only problem remaining is it is places a link to phrases without "www" or "http".

e.g.

"52.90/month"

thank you
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
"52.90/month"
If you can explain to us in plain language why that would be a URL instead of a piece of text, we can write a regular expression rule for you.  This is the problem with trying to parse natural language and computer language at the same time.  It's just not very effective.

Test data, please!
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html
0
 

Author Comment

by:myyis
Comment Utility
I mean the function of Gary puts a link to this phrase
"52.90/month".
It should leave as text.
0
 
LVL 58

Accepted Solution

by:
Gary earned 300 total points
Comment Utility
Change the pattern to

$pattern = '#(www\.|https?://)[\w\d]+\.[\d\w]{2}\S*#i';
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
One more time... Test data, please!
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

This is not easy when we have to guess what your test data consists of!   However if you follow the design patterns in the article you can make up a regular expression that will be workable for most of your needs.  It's not quite rocket science, but close, unless you use the stable and predictable patterns for test driven development.  And then it's easy!

Going forward you might want to learn about PHPunit.  It's the current state of the art.
0
 

Author Comment

by:myyis
Comment Utility
Hi Ray,
Actually I do a test using http://sandbox.onlinephpfunctions.com/
But in your answer the problem was a bug at my site.
Thank you for the info, and sorry for the time you spent.
Gary, the code worked thank you.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now