GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum > PHP Code Library
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Jan-2004, 05:02
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

[function] AutoLink (converts URLS into links inside a string)


The PHP function autolink( $str ) described below will parse the string ($str) you feed it, convert any (OK, most.. ;)) URLs found inside the string or text and turn them into clickable links to display on a web page.

About the function: autolink():
  • It requires 2 other functions to work; i.e. _autolink_find_URLS() and _autolink_create_html_tags().
  • $str in the main function is passed by reference, so it returns nothing (void).
  • It is recommended that you pass the STRING $str through $str = htmlspecialchars( $str ); or $str = htmlentities( $str ); before you run it through this function.

The required 3 functions:

PHP Code:

<?php
/**
   NAME        : autolink()
   VERSION     : 1.0
   AUTHOR      : J de Silva
   DESCRIPTION : returns VOID; handles converting
                 URLs into clickable links off a string.
   TYPE        : functions
   ======================================*/

function autolink( &$text, $target='_blank', $nofollow=true )
{
  // grab anything that looks like a URL...
  $urls  =  _autolink_find_URLS( $text );
  if( !empty($urls) ) // i.e. there were some URLS found in the text
  {
    array_walk( $urls, '_autolink_create_html_tags', array('target'=>$target, 'nofollow'=>$nofollow) );
    $text  =  strtr( $text, $urls );
  }
}

function _autolink_find_URLS( $text )
{
  // build the patterns
  $scheme         =       '(http:\/\/|https:\/\/)';
  $www            =       'www\.';
  $ip             =       '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
  $subdomain      =       '[-a-z0-9_]+\.';
  $name           =       '[a-z][-a-z0-9]+\.';
  $tld            =       '[a-z]+(\.[a-z]{2,2})?';
  $the_rest       =       '\/?[a-z0-9._\/~#&=;%+?-]+[a-z0-9\/#=?]{1,1}';            
  $pattern        =       "$scheme?(?(1)($ip|($subdomain)?$name$tld)|($www$name$tld))$the_rest";
    
  $pattern        =       '/'.$pattern.'/is';
  $c              =       preg_match_all( $pattern, $text, $m );
  unset( $text, $scheme, $www, $ip, $subdomain, $name, $tld, $the_rest, $pattern );
  if( $c )
  {
    return( array_flip($m[0]) );
  }
  return( array() );
}

function _autolink_create_html_tags( &$value, $key, $other=null )
{
  $target = $nofollow = null;
  if( is_array($other) )
  {
    $target      =  ( $other['target']   ? " target=\"$other[target]\"" : null );
    // see: http://www.google.com/googleblog/2005/01/preventing-comment-spam.html
    $nofollow    =  ( $other['nofollow'] ? ' rel="nofollow"'            : null );     
  }
  $value = "<a href=\"$key\"$target$nofollow>$key</a>";
} 

?>



Example Code / Use

PHP Code:

<?php
// let's say we have a dummy string read from a file or db
$text = 'For more information, visit our website http://www.desilva.biz or 
www.gidforums.com/ to discuss this. This topic is discussed here: 
http://www.gidforums.com/t-1787.html';


// first we pass it through htmlentities()
$text = htmlentities( $text );

// include the 3 functions above
include_once( 'includes/the_3_functions_above_are_in_this_file.php' );
// NOW we run it through autolink()!
autolink( $text );
// display the text in a web page
echo $text."<br />\r\n";
?>



Want to discuss these PHP functions? Your comments, corrections, suggestions are welcome here: Clickable web-urls from database.
 
 

Recent GIDBlogGetting Rid of Facebook Chat Sidebar by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[function] Show limited words of a string / text JdS PHP Code Library 0 05-Jun-2003 09:25

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:31.


vBulletin, Copyright © 2000 - 2012, Jelsoft Enterprises Ltd.