Search
Left Quote    Knowledge speaks, but wisdom listens.
- Jimi Hendrix    
Right Quote
 
[login] | [Register]
Forum Index » PHP and mySQL » Viewing Topic and Replies
Post Reply


        Viewing Topic: Need help editting an Auto-link function
  This user is offline  b321618
  Subject: "Need help editting an Auto-link fun..." Posted: @ 8:38 am on Oct 17 2009   
Member #: 946
Rank: User - (7)
Since: 05/28/09
Posts: 7

Hello again, found this code for auto-linking, got it to work, but Id like to make it avoid links that are between certain tags, like if someone added the <a> tags without realizing it already tagged them, and to avoid the object param and embed tags, so videos will post without being disturbed by the auto-linking. Thanks for your time! I refer your page all the time!


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_crea te_html_tags', array('target'= >$target, 'nofollow'=> $nofollow) );
$text = strtr( $text, $urls );
}
}

function _autolink_find_ URLS( $text )
{
// build the patterns
$scheme = '(http:\/\/|htt ps:\/\/)';
$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|($subd omain)?$name$tl d)|($www$name$t ld))$the_rest&q uot;;

$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_creat e_html_tags( &$value, $key, $other=null )
{
$target = $nofollow = null;
if( is_array($other ) )
{
$target = ( $other['target' ] ? " target=\"$ other[target]\& quot;" : null );
// see: http://www.goog le.com/googlebl og/2005/01/prev enting-comment- spam.html
$nofollow = ( $other['nofollo w'] ? ' rel="nofol low"' : null );
}
$value = "<a href=\"$ke y\"$target $nofollow>$k ey</a>&qu ot;;
}

?>




    Viewed: 2,131 Times | Reply to This | To top
Viewing Page: 1 of 1



You must be logged in to post on the forums. Login or Register








"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank