How can I auto link URLs with simplepie

Discussion in 'PHP' started by crazyfish, Apr 23, 2009.

  1. #1
    I am displaying some twitter feeds using simplepie but I have notice that a lot of the tweets have link in them so I can I make them 'clickable' URLs?

    To display the tweet I am using this code:

    <?php echo $item->get_title(); ?>
    Code (markup):
    I haven't been able to find a solution.

    Thanks in advance!
     
    crazyfish, Apr 23, 2009 IP
  2. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    hmm, doesn't
    <?php echo "<a href='" . $item->get_title() . "'>" . $item->get_title() . "</a>"; ?>
    Code (markup):
    work at all?
     
    szalinski, Apr 23, 2009 IP
  3. crazyfish

    crazyfish Active Member

    Messages:
    1,110
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    80
    #3
    that links the whole tweet to twitter, i want to link the urls that people post in the tweet, like something to tinyurl.com.
     
    crazyfish, Apr 23, 2009 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    You would probably need to parse the tweet, look for http:// in the tweet, and if found, add tags to display the URL with <a href> tags. preg_replace would probably work here.

    I'm not familiar enough with regexp to make the actual pattern for recognizing the urls, but it would be something like this:
    $items = preg_replace("<pattern>","<pattern for replacement>",$item->get_title());

    and then you could just use "$items" instead of $item->get_title() in your code
     
    PoPSiCLe, Apr 23, 2009 IP
  5. crazyfish

    crazyfish Active Member

    Messages:
    1,110
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    80
    #5
    That got me enough info to get it to work. Although the link text only shows the mydomain.com and nothing else but it does link to the right URL. Here is the final code:

    <?php $items = preg_replace( '/(http|ftp)+(s)?:(\/\/)((\w|\.)+)(\/)?(\S+)?/i', '<a href="\0">\4</a>', $item->get_title());
    		echo $items; 
    		 ?>
    Code (markup):
     
    crazyfish, Apr 23, 2009 IP