Solved

make url clickable

Posted on 2014-07-17
19
330 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
ID: 40202828
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
ID: 40202869
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
ID: 40202885
<?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
ID: 40202924
0
 
LVL 58

Expert Comment

by:Gary
ID: 40202934
Works for me.
0
 

Author Comment

by:myyis
ID: 40202939
0
 
LVL 58

Expert Comment

by:Gary
ID: 40202944
Sample with link

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

Expert Comment

by:Gary
ID: 40202952
http://3v4l.org/JvBOW

Previous one had an erroneous carriage return.
0
 

Author Comment

by:myyis
ID: 40202954
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 58

Expert Comment

by:Gary
ID: 40203018
$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
ID: 40203079
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
ID: 40203106
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
ID: 40203409
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
ID: 40203411
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
ID: 40203436
"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
ID: 40203444
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
ID: 40203451
Change the pattern to

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

Expert Comment

by:Ray Paseur
ID: 40203488
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
ID: 40203498
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 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

16 Experts available now in Live!

Get 1:1 Help Now