Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make text url to hyperlink using PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.55k
    Comment on it

    Hello Reader!, On printing the url in plain text, they do not appears as hyper linked but using PHP you can perform this.
    Let's see the example below that takes a plan url as input and return the hyperlinked url:-

    <?
    function createHyperLink($url)
    {
                $url = html_entity_decode($url);
            $url = " ".$url;
            $url = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
                    '<a href="\\1" target=_blank>\\1</a>', $url);
            $url = eregi_replace('(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
                    '<a href="\\1" target=_blank>\\1</a>', $url);
            $url = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
            '\\1<a href="http://\\2" target=_blank>\\2</a>', $url);
            $url = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
            '<a href="mailto:\\1" target=_blank>\\1</a>', $url);
            return $url;
    }
    
    // Example Usage
    
    echo createHyperLink("Hello this is url hyperlinking performing task on findnerd.com.  Also it made the plan text of email id into likened mails as admin@findnerd.com");
    ?>
    

    Now in the output the code above, the url will appeared as a linked and email will appeared as linked mail (mailto:)

 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: