Call us Toll-Free:
1-800-218-1525
Email us

 Sponsors

Add Anchor Tags to urls

Dawn Rossi, 12-07-2007
This little function will add anchor href tags to urls, converting all links to anchor tags with a redirect under your domain


function IsValidChar($ch,$allow_space=0)
{
    if (
$ch=='=' && $allow_space) return 1;

    return ((
$ch>='0' && $ch<="9") ||
        (
$ch>='a' && $ch<='z') ||
        (
$ch=='.' || $ch=='-' || $ch=='/' || $ch==':' || $ch=='_' || $ch==';' || $ch=='?' || $ch=='&') ||
        (
$ch>='A' && $ch<='Z'));
}

function
IsSpaceChar($ch)
{
    return (
$ch=="'" || $ch==' ' || $ch=="\r" || $ch=='"' || $ch=="\n" || $ch=="\t");
}

           
function
IsLink(&$text, $pos_start, $pos_end, &$prevchar)
{       
   
// Init       
   
$prevchar = "";
       
   
// If this is a link, it should be prefixed by
    // href= or URL= 
    // Advance to the first character before our link
   
$pos_prevchar = $pos_start;
    if (
$pos_prevchar>0) $pos_prevchar--;     
    while (
$pos_prevchar>0 && IsSpaceChar($text[$pos_prevchar]))
    {
       
$pos_prevchar--;
    }
       
   
// It's a link if pattern is URL= or HREF=
   
if ($text[$pos_prevchar]=='=' && $pos_prevchar>0)
    {
       
$pos_equal = $pos_prevchar;   
       
$pos_prevchar--;
        while (
$pos_prevchar>0 && !IsSpaceChar($text[$pos_prevchar])) $pos_prevchar--;
             
       
$command = trim(substr($text, $pos_prevchar,$pos_equal-$pos_prevchar));
           
       
$prevchar = "=";
               
        return (
Strcasecmp($command,"url")!=0 && Strcasecmp($command,"href")!=0);
         
           
    }           
               
    return
0;
}

function
IsNotSupportedLink($link)
{
    if (
strpos($link,"ftp:")!==false ) return 1;
             
    if (
strpos($link,".com")===false && strpos($link,".net")===false &&
   
strpos($link,".co.il")===false && strpos($link,".co.uk")===false) return 1;
       
    return
0;
}


function
FixVBShortLinks(&$text)
{
   
$temp = $text;
   
$text = "";

    do
    {


        if ((
$pos = _strpos($temp,"[url]"))==-1) break;

       
$pos_end = _strpos($temp,"[/url]");
        if (
$pos_end==-1) break;

       
$link = substr($temp, $pos+5, $pos_end-($pos+5));

       
$text .= substr($temp,0,$pos)."[UR"."L=\"$link\""."]".$link."["."/UR"."L]";
       
$temp = substr($temp,$pos_end+6);

    } while (
1);

   
$text .= $temp;

    return
1;
}

function
stri_replace( $find, $replace, $string )
{
   
$new_string = "";
 
   
// Validate
   
if (empty($find))
    {
        return
$string;
    }

   
$cnt=0;
   
$length = strlen($string);
    for (
$i=0; $i<$length; $i++)
    {
       
// If this is a character match
       
if (strtolower($string[$i]) == strtolower($find[$cnt]))
        {
           
// Increase counter
           
$cnt++;

           
// If this is a full match
           
if ($cnt >= strlen($find))
            {
               
// Reset
               
$cnt = 0;

               
// Add to our new string
               
$new_string .= $replace;
            }
        }
       
// (Else - not a character match)
       
else
       
// Just add it
       
{
           
// If we have any pending leftovers
           
while ($cnt>0)
            {
               
$new_string .= $string[$i-$cnt];
               
$cnt--;
            }

           
$new_string .= $string[$i];
        }
    }

   
// If we have any pending leftovers
   
if ($cnt>0)
    {
       
// Add it
       
while ($cnt>0)
        {
           
$new_string .= $string[strlen($string)-$cnt];
           
$cnt--;
        }
    }

  return(
$new_string );
}

function
AddAnchorTags($text,$vbulletin_url=0)
{
   
FixVBShortLinks(&$text);

   
$REDIRECT = "http://www.mydomain.com/go/"
   
$temp = $text;
   
$text = "";
       
    do   
    {
        if ((
$pos = strpos($temp,"."))===false) break;
           
       
// Set these for easier access
       
$pos_start = $pos;
               
       
// We have something that looks like a URL,
        // Advance to the end of the string so that we can determine the TLD
       
while ($pos
       
{
            if (
IsValidChar($temp[$pos+1],1)) $pos++;
            else break;
        }
       
$pos_end = $pos;   
       
       
// Advance to the beginning of the string
       
$pos = $pos_start;
        while (
$pos>0)
        {
            if (
IsValidChar($temp[$pos-1])) $pos--; 
            else break;
        }
       
$pos_start = $pos;
       
// If string starting at pos_start and ending at pos_end is already a link,
       
if (IsLink(&$temp, $pos_start, $pos_end, &$prevchar) || IsNotSupportedLink($link)
        ||
strpos($link,$REDIRECT)!==false)
        {
           
// Skip
           
$text .= substr($temp,0,$pos_end+1);
           
$temp = substr($temp,$pos_end+1);
        }
       
// Else - not a link
       
else
        {

       
// Strip http/https
       
$link = stri_replace("http://","",$link);
       
$link = stri_replace("https://","",$link);

            if (
$prevchar!='=' )
            {
                if (
$vbulletin_url) $link = "[url=".'"'.$REDIRECT.$link.'"'."]".$link."["."/URL]";
                else
$link = "$link";
            }
            else
$link = "$REDIRECT$link";

           
$text .= substr($temp,0,$pos_start).$link;
           
$temp = substr($temp, $pos_end+1);
        }

    } while (
1);

   
$text .= $temp;

    return
$text;
}

Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

 Comments
Post your comments












Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.



About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions