coding for food

a blog by @gregavola

  • Home
  • About Me
    • Edit
    • Delete
    • Autopost

    Improving CodeIgniter's auto_link() function for Twitter Hashtags and Usernames

    I've stared using CodeIgniter's framework to develop all my application with intent of learning more about the MVC frame work. However, sometimes it's hard to implement features that I currently had working without using the frame work. For example - the auto_link() function in CodeIngiter. It's amazing feature that turns string URLs into "click-able" URLs. This made parsing Tweets that much easier - but what if I wanted to parse the Twitter username and Hash tags as well?

    Well I wrote this small, quick and dirty script that you can add the auto_link() function inside the url_helper.php file in system/helpers. Just add it in before the "return $str" and you'll be parsing links in no time!

    
    
    if ($type != 'twitter')
    {           
       $search = array('|#([\w_]+)|', '|@([\w_]+)|');
       $replace = array('#$1', '@$1');
       $str =  preg_replace($search, $replace, $str);
    }
    
    
    // Usage
    
    
    echo auto_link("@gregavola is #awesome", "twitter");
    
    
    

    Enjoy!

    UPDATE: As n0xie noted on Twitter, it is better to extend the auto_link() function as opposed to edited it directly. You can accomlish this by creating a MY_url_helper.php file inside the system/helpers folders and and override the auto_link() function by adding the text above to the orginal function. This way when auto_link() is called, it get overriden by this function you created in MY_url_helper.php.

    • 7 April 2010
    • Views
    • 0 Comments
    • Permalink
    • Favorited 0 Times
    • Tweet

    Comments 0 Comments

    Leave a Comment

  • Greg Avola's Posterous


  • About Greg Avola


  • Subscribe

    Subscribe to this posterous
    Unsubscribe
    Follow this posterous RSS
  • Follow Me

         

Theme created for Posterous by Obox