Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

make url clickable

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
myyis
Asked:
myyis
  • 8
  • 7
  • 4
2 Solutions
 
Ray PaseurCommented:
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
 
myyisAuthor Commented:
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
 
GaryCommented:
<?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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
myyisAuthor Commented:
0
 
GaryCommented:
Works for me.
0
 
GaryCommented:
Sample with link

http://3v4l.org/rHhiq
0
 
GaryCommented:
http://3v4l.org/JvBOW

Previous one had an erroneous carriage return.
0
 
myyisAuthor Commented:
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
 
GaryCommented:
$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
 
myyisAuthor Commented:
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
 
GaryCommented:
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
 
Ray PaseurCommented:
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
 
myyisAuthor Commented:
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
 
Ray PaseurCommented:
"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
 
myyisAuthor Commented:
I mean the function of Gary puts a link to this phrase
"52.90/month".
It should leave as text.
0
 
GaryCommented:
Change the pattern to

$pattern = '#(www\.|https?://)[\w\d]+\.[\d\w]{2}\S*#i';
0
 
Ray PaseurCommented:
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
 
myyisAuthor Commented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now