Hot Koehls
  • Email
  • Feedburner
  • Linkedin
  • Twitter
  • Home
  • About
  • Archives
  • Contact
  • Software
    • S3imple Backup
    • Twitter Feed Archiver
    • FileTime
    • Flickr API Demo
Search
Home» For everyone » Parse URL’s in text, create links

Parse URL’s in text, create links

Posted by Frank - December 15, 2008 - For everyone, For techies
8

I’m absolutely in love with the status update stream I’ve put together for Fwd:Vault (follow link for example). However in the process, I’ve discovered a huge drawback to the Twitter messaging system: it does not store links. The Twitter site itself will identify URL’s in messages and convert them into clickable links for you automatically. But the magic ends at Twitter’s borders; anyone who wants to do the same on their site is on their own.

So I consulted the almighty Google. I found plenty of raw regex, javascript, and Twitter-focused discussions on the matter, but I found the offered solutions and tips lacking. I wanted to do this up right, transparently via PHP in the background. No JS required.

Finally, I found a small PHP script that accomplished what I needed. Here’s a renamed version—all code intact—that will find and convert any well-formed URL into a clickable <a> tag link.

Update: My buddy Tonk has updated the code to link up @replies and #hashtags as well. He also switched from POSIX to Perl regular expressions syntax, mostly cause he’s a regex dork.

function linkify( $text ) {
  $text = preg_replace( '/(?!<\S)(\w+:\/\/[^<>\s]+\w)(?!\S)/i', '$1', $text );
  $text = preg_replace( '/(?!<\S)#(\w+\w)(?!\S)/i', '#$1', $text );
  $text = preg_replace( '/(?!<\S)@(\w+\w)(?!\S)/i', '@$1', $text );
  return $text;
}

Copy that into your code, then run your text containing unlinked URL’s through it. Let’s apply it to the Twitter feed example as we left it in Step 2:

  • text) . '' . $time_display; ?>
  • You can find this code at work on Fwd:Vault.

    Build a slick Twitter feed on your site

    1. Display Twitter updates on your website
    2. Calculate dates and times in different timezones (translate Twitter timestamps)
    3. Parse URL’s in text, create links
    fwdvault, php, regular expressions, Twitter

    8 comments on “Parse URL’s in text, create links”

    1. Dan says:
      July 14, 2009 at 5:26 pm

      Had trouble with your code, lost the link text and it Linked ever Tweet below with a false link. I tried this out and it worked Great, not sure the difference…
      (It links the Hash Tags and the @danfoxx too.)

      function linkify($status_text)
      {
      // linkify URLs
      $status_text = preg_replace(
      ‘/(https?:\/\/\S )/’,
      ‘\1‘,
      $status_text
      );

      // linkify twitter users
      $status_text = preg_replace(
      ‘/(^|\s)@(\w )/’,
      ‘\1@\2‘,
      $status_text
      );

      // linkify tags
      $status_text = preg_replace(
      ‘/(^|\s)#(\w )/’,
      ‘\1#\2‘,
      $status_text
      );

      return $status_text;
      }

    2. Matt says:
      July 14, 2009 at 5:50 pm

      I too had some issues with the original code. Dan, I tried your code and still could not get it to work. Are you still using the same output method?

    3. Matt says:
      July 14, 2009 at 6:27 pm

      I tried this. I changed the name of variable from $t to $tweettext and then I updated the ereg_replace statement.

      function linkify($tweettext) {
      $tweettext = ereg_replace(“[[:alpha:]] ://[^[:space:]] [[:alnum:]/]”,”\“, $tweettext);
      return $tweettext;
      }

      It worked for me. Although I am real interested to see how Dan’s solution works.

    4. Dan says:
      July 14, 2009 at 9:37 pm

      Here is my Entire Code… Works great, check out the Links Page of my site…

      http://thefoxxfamily.com/

      CODE:

      <?php
      function twitter_status($twitter_id, $hyperlinks = true) {
      $c = curl_init();
      curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/15456263.xml&quot ;) ;
      curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 3);
      curl_setopt($c, CURLOPT_TIMEOUT, 5);
      $response = curl_exec($c);
      $responseInfo = curl_getinfo($c);
      curl_close($c);
      if (intval($responseInfo['http_code']) == 200) {
      if (class_exists('SimpleXMLElement')) {
      $xml = new SimpleXMLElement($response);
      return $xml;
      } else {
      return $response;
      }
      } else {
      return false;
      }
      }

      function linkify_twitter_status($status_text)
      {
      // linkify URLs
      $status_text = preg_replace(
      '/(https?:\/\/\S )/',
      '\1‘,
      $status_text
      );

      // linkify twitter users
      $status_text = preg_replace(
      ‘/(^|\s)@(\w )/’,
      ‘\1@\2‘,
      $status_text
      );

      // linkify tags
      $status_text = preg_replace(
      ‘/(^|\s)#(\w )/’,
      ‘\1#\2‘,
      $status_text
      );

      return $status_text;
      }

      if ( $twitter_xml = twitter_status(’12345678′) ) {
      date_default_timezone_set(‘GMT’);
      $tz = new DateTimeZone(‘America/New_York’);
      $i = 0;
      foreach ($twitter_xml->status as $key => $status) {
      $datetime = new DateTime($status->created_at);
      $datetime->setTimezone($tz);
      $time_display = $datetime->format(‘D, M jS g:ia T’);
      ?>
      text) . ” . $time_display; ?>

      read more updates…

    5. Frank says:
      July 14, 2009 at 9:40 pm

      Stupid WordPress editor mangled the code. It’s fixed, try the above.

    6. Chris says:
      July 30, 2009 at 9:53 pm

      I’ve updated the linkify() function to include support for turning #hashtags into links as well (excuse my having converted from ereg_* to preg_* – just a comfort-zone thing).

      function linkify( $text ) {
        $text = preg_replace( '/(?!< \S)(\w+:\/\/[^<>\s]+\w)(?!\S)/i', '$1', $text );
        $text = preg_replace( '/(?!< \S)#(\S+\w)(?!\S)/i', '#$1', $text );
        return $text;
      }
      
    7. Chris says:
      August 4, 2009 at 4:10 pm

      Here’s an[other] updated linkify() function that turns @account tokens into Twitter account links as well. Let’s hope WordPress doesn’t mangle this one too badly…

      function linkify( $text ) {
        $text = preg_replace( '/(?!<\S)(\w+:\/\/[^<>\s]+\w)(?!\S)/i', '<a href="$1" target="_blank">$1</a>', $text );
        $text = preg_replace( '/(?!<\S)#(\w+\w)(?!\S)/i', '<a href="http://twitter.com/search?q=#$1" target="_blank">#$1</a>', $text );
        $text = preg_replace( '/(?!<\S)@(\w+\w)(?!\S)/i', '@<a href="http://twitter.com/$1" target="_blank">$1</a>', $text );
        return $text;
      }
      
    8. ChicagoBrown says:
      March 20, 2011 at 1:04 am

      This code is awesome, thanks for the help!

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    Categories

    • For entrepreneurs
    • For everyone
    • For techies

    Latest Tweets

    • The word traps planners plan themselves into | Life. Then strategy http://t.co/iANAdASb
      May 8, 2012 - 2:43 pm
    • Random network security tip for those about to appear on TV - Boing Boing http://t.co/tC1lXFQ4
      May 8, 2012 - 1:42 pm
    • A Picture http://t.co/H846Uy69
      April 27, 2012 - 12:25 pm
    • The Broken "Buy-One, Give-One" Model: 3 Ways to Save Toms Shoes | Co.Exist: World changing ideas and innovation http://t.co/RI0sVMW6
      April 10, 2012 - 12:23 pm

    Recent Comments

    • whiz on What 255 characters looks like
    • Andrew on Find the second (or third, or fourth) occurence in a string
    • IanArcher on Get number of message parts in an email using PHP
    • Usama on Remove parent directories from tar archives
    • Frank on It’s dangerous to go alone

    Recent Posts

    • It’s dangerous to go alone
    • Create Self-Signed Wildcard SSL Certificate
    • What comes after the yottabyte?
    • Write code like they do in Hollywood
    • Brian Rolle machine gun celebration
    (c) 2012 Frank Koehl. All Rights Reserved.
    • Contact Us
    • Sitemap