Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to set anchor on email and urls using PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 110
    Comment on it

    Hello Reader's! If you are looking for code that make your plain MyStringPlainText urls and emails into anchor format then you can use the code function as below:-

    <?
    function SetTheAnchor($MyStringPlainText)
    {
    
            $MyStringPlainText = html_entity_decode($MyStringPlainText);
            $MyStringPlainText = " ".$MyStringPlainText;
            $MyStringPlainText = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
                    '<a href="\\1" target=_blank>\\1</a>', $MyStringPlainText);
            $MyStringPlainText = eregi_replace('(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
                    '<a href="\\1" target=_blank>\\1</a>', $MyStringPlainText);
            $MyStringPlainText = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
            '\\1<a href="http://\\2" target=_blank>\\2</a>', $MyStringPlainText);
            $MyStringPlainText = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
            '<a href="mailto:\\1" target=_blank>\\1</a>', $MyStringPlainText);
            return $MyStringPlainText;
    }
    
    
    echo SetTheAnchor("Pass this link to my convert anchor string : http://www.findnerd.com  Or you can mail me here admin@abc.com");
    ?>
    

    The PHP code above will use the string that you pass in SetTheAnchor() function and if any email of url is found in it, it will set the anchor on them and make it clickable. Similarly if any email is found it will link your outlook on clicking it.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: