.

ConverterTwitter Class Reference

Converte a twitter message into HTML. More...

Inheritance diagram for ConverterTwitter:
IConverter

List of all members.

Public Member Functions

  decode ($value, $params=false)
  encode ($value, $params=false)

Public Attributes

const  EXPAND_LINKS = 1024

Detailed Description

Converte a twitter message into HTML.

Replaces hashes with .., user (@..) by .. and links by an anchor tag

Optionally, if ConverterTwitter::EXPAND_LINKS is passed as parameter, The component will try to resolve redirect services like bit.ly et al, replacing the url in the message with the resolving one.

Author:
Gerd Riesselmann

Definition at line 17 of file twitter.converter.php.


Member Function Documentation

ConverterTwitter::decode ( value,
params = false  
)

Implements IConverter.

Definition at line 62 of file twitter.converter.php.

00062                                                         {
00063                 $value = str_replace('>', ">", $value);
00064                 $value = str_replace('&lt;', "<", $value);
00065                 return trim($value);            
00066         }               
ConverterTwitter::encode ( value,
params = false  
)

Implements IConverter.

Definition at line 20 of file twitter.converter.php.

00020                                                         {
00021                 $value = String::escape($this->decode($value));
00022                 // Try to find hash tags and make them bold
00023                 $search = '@(\s#[\S]*)@';
00024                 $replace = ' <b>$1</b>';
00025                 $value = String::preg_replace($search, $replace, $value);
00026 
00027                 // Try to find users and make them italic
00028                 $search = '|(@[\S]*)|';
00029                 $replace = '<i>$1</i>';
00030                 $value = String::preg_replace($search, $replace, $value);
00031                         
00032                 // Replace URLS
00033                 $search = "@(http[s]?://[\S]*)@";
00034                 
00035                 if (Common::flag_is_set($params, self::EXPAND_LINKS)) {
00036                         // FInd them to check for bit.ly et al
00037                         $matches = array();
00038                         String::preg_match_all($search, $value, $matches);
00039                         
00040                         Load::components('httprequest');
00041                         $err = new Status();
00042                         foreach (Arr::get_item($matches, 0, array()) as $url) {
00043                                 $head = HttpRequest::get_head($url, $err);
00044                                 // Look for a location header element
00045                                 $search_loc = '@location: ([\S]*)@i';
00046                                 $matches_loc = array();
00047                                 if (String::preg_match_all($search_loc, $head, $matches_loc)) {
00048                                         // Get last redirect
00049                                         $new_url = String::escape(array_pop($matches_loc[1]));
00050                                         $value = str_replace($url, $new_url, $value);   
00051                                 }                       
00052                         }
00053                 }
00054                 
00055                 $replace =  '<a href="$1">$1</a>';
00056                 $value = String::preg_replace($search, $replace, $value);
00057                 
00058                 return $value;
00059         }

Member Data Documentation

Definition at line 18 of file twitter.converter.php.


The documentation for this class was generated from the following file: