SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Oct 2012
    Location
    San Diego, Ca
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    convert text urls on page to clickable urls

    I have a site page with a lot of text urls and the client wants them active, is there a code i can add to the page to convert those text links to live click-able HTML links?

    If so I'm fairly new to PHP and would like info on how to add it to my site.

    Thanks

    Cisco115

  2. #2
    SitePoint Enthusiast edgarzakaryan's Avatar
    Join Date
    Aug 2012
    Location
    Yerevan, Armenia
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb

    Hello.

    Here is the script you need.

    It will change all emails and urls in text

    Code:
    <?php
    
    $text = "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece es of Good  comes from a line in section 1.10.32.
    
    The standard chunk of Lorem http://google.com Ipsum used since  example@gmail.com the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from e also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.";
    
    
    
    function  autolink($message) { 
        //Convert all urls to links
        $message = preg_replace('#([\s|^])(www)#i', '$1http://$2', $message);
        $pattern = '#((http|https|ftp|telnet|news|gopher|file|wais):\/\/[^\s]+)#i';
        $replacement = '<a href="$1" target="_blank">$1</a>';
        $message = preg_replace($pattern, $replacement, $message);
    
        /* Convert all E-mail matches to appropriate HTML links */
        $pattern = '#([0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.';
        $pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i';
        $replacement = '<a href="mailto:\\1">\\1</a>';
        $message = preg_replace($pattern, $replacement, $message);
        return $message;
    }
    
    echo autolink($text);

  3. #3
    SitePoint Member
    Join Date
    Oct 2012
    Location
    San Diego, Ca
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool Thanks for the script, I'm new to php, where on the page or how do i link it to page?

    Hi ,

    Thanks for the script, I'm new to php, how do i install this to work on my page.
    That's my biggest question, I would appreciate it.

    Thanks


    Quote Originally Posted by edgarzakaryan View Post
    Hello.

    Here is the script you need.

    It will change all emails and urls in text

    Code:
    <?php
    
    $text = "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece es of Good  comes from a line in section 1.10.32.
    
    The standard chunk of Lorem http://google.com Ipsum used since  example@gmail.com the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from e also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.";
    
    
    
    function  autolink($message) { 
        //Convert all urls to links
        $message = preg_replace('#([\s|^])(www)#i', '$1http://$2', $message);
        $pattern = '#((http|https|ftp|telnet|news|gopher|file|wais):\/\/[^\s]+)#i';
        $replacement = '<a href="$1" target="_blank">$1</a>';
        $message = preg_replace($pattern, $replacement, $message);
    
        /* Convert all E-mail matches to appropriate HTML links */
        $pattern = '#([0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.';
        $pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i';
        $replacement = '<a href="mailto:\\1">\\1</a>';
        $message = preg_replace($pattern, $replacement, $message);
        return $message;
    }
    
    echo autolink($text);


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •