SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Something for all you.

    I just though I post something that may be useful for someone.

    I was looking for a function that would take in a string of text like maybe an email in plaintext format, then find strings that look like urls and turn them into clickable links.

    I found one such function on the web. it was just copied from one website to the other and everyone was just posting the same code that sucked.

    I made some changes and now it works properly. See if you like it
    PHP Code:
    function makeClickable($text$maxurl_len 50$addNofollow true$target '_blank')
    {
        
        if (
    preg_match_all('/((ht|f)tps?:\/\/([\w\.]+\.)?[\w\-_?&=;]+(\.[a-zA-Z]{2,4})?[^\s\r\n\(\)"\'<>\,\!]+)/si'$text$urls))
        {
            
    $offset1 ceil(0.65 $maxurl_len) - 2;
            
    $offset2 ceil(0.30 $maxurl_len) - 1;

            
    $nofollow = (true === $addNofollow) ? 'rel="nofollow"' '';
            
    $aUrls = (array_unique($urls[1]));

            foreach (
    $aUrls as $url){
                if (
    $maxurl_len && (strlen($url) > $maxurl_len)){
                    
    $urltext substr($url0$offset1) . '...' substr($url, -$offset2);
                }else{
                    
    $urltext $url;
                }

                
    $pattern '~([^\w]+)'.preg_quote($url).'([^\w]+)~'
            
                
    $text preg_replace($pattern'\\1<a href="'$url .'" '.$nofollow.' target="'$target .'">'$urltext .'</a>\\2'$text);

            }
        }

        return 
    $text;
            

    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  2. #2
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    I love it! Thanks for the share.
    TAKE A WALK OUTSIDE YOUR MIND.

  3. #3
    SitePoint Zealot Aimhigh's Avatar
    Join Date
    Jul 2008
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for sharing. this is very helpful.

  4. #4
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lampcms.com View Post
    I just though I post something that may be useful for someone.

    I was looking for a function that would take in a string of text like maybe an email in plaintext format, then find strings that look like urls and turn them into clickable links.

    I found one such function on the web. it was just copied from one website to the other and everyone was just posting the same code that sucked.

    I made some changes and now it works properly. See if you like it
    PHP Code:
    function makeClickable($text$maxurl_len 50$addNofollow true$target '_blank')
    {
        
        if (
    preg_match_all('/((ht|f)tps?:\/\/([\w\.]+\.)?[\w\-_?&=;]+(\.[a-zA-Z]{2,4})?[^\s\r\n\(\)"\'<>\,\!]+)/si'$text$urls))
        {
            
    $offset1 ceil(0.65 $maxurl_len) - 2;
            
    $offset2 ceil(0.30 $maxurl_len) - 1;

            
    $nofollow = (true === $addNofollow) ? 'rel="nofollow"' '';
            
    $aUrls = (array_unique($urls[1]));

            foreach (
    $aUrls as $url){
                if (
    $maxurl_len && (strlen($url) > $maxurl_len)){
                    
    $urltext substr($url0$offset1) . '...' substr($url, -$offset2);
                }else{
                    
    $urltext $url;
                }

                
    $pattern '~([^\w]+)'.preg_quote($url).'([^\w]+)~'
            
                
    $text preg_replace($pattern'\\1<a href="'$url .'" '.$nofollow.' target="'$target .'">'$urltext .'</a>\\2'$text);

            }
        }

        return 
    $text;
            

    did you try for all possible url structure?

  5. #5
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, urlencoded urls will not be picked up, the regex does not include the &#37; and + chars
    It's easy to add them though:
    replace
    [\w\-_?&=;]
    with
    [\w\-_?&=;%+] // Don't forget to test if you change anything in the regex!

    The problem with the old version that I based this function on is that the old one user str_replace and it could not handle situations where a document contained 2 links that were partially the same, for example, if document contained
    http://www.php.net and http://www.php.net/str_replace

    then if the longer url appeared before the shorter one, the script would replace only the first part of it, it would not be clickable and would look nasty.

    My version uses preg_replace with takes care of situations like that and can handle docs in which the same link appears multiple time and where links point to the same domain.
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!


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
  •