SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Convert URL in string to proper link

    hi, I am creating a module to read rss feed of twitter account. Most of the time people post shortened links in their status.

    I would like to convert the URLs in status to proper links.

    Could you please let me know how this can be done?

    Thanks in anticipation.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function make_clickable($text) {
        
    $ret ' ' $text;
        
    $ret preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise""'\\1<a href=\"\\2\" >\\2</a>'"$ret);
        
    $ret preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise""'\\1<a href=\"http://\\2\" >\\2</a>'"$ret);
        
    $ret preg_replace("#(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i""\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>"$ret);
        
    $ret substr($ret1);
        return 
    $ret;

    This function should handle it for you. I use it on w3roi to show a Twitter feed with clickable links.

  3. #3
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks Dan! I will try it out.

    Meanwhile I have noticed another thing that some people also post unicode characters in their tweets. Could you please let me know how I can show these characters properly. Right now they appear something like ♥.

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You need to make sure your feed parser is multibyte safe, that you serve your webpage in the same encoding as you got the feed (probably UTF-8 for Unicode), and that you don't touch the string with any non-multibyte-safe string functions before displaying it.

  5. #5
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    You need to make sure your feed parser is multibyte safe, that you serve your webpage in the same encoding as you got the feed (probably UTF-8 for Unicode), and that you don't touch the string with any non-multibyte-safe string functions before displaying it.
    oh wow, I was not accepting that quick response.

    could you please send a link where I can read about multibyte safe functions and changing the page encoding?

    Thanks alot!


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
  •