PHP Classes

Demo to replace URLS and Images with nice HTML code

Recommend this page to a friend!

      Twitter API PHP Class  >  All threads  >  Demo to replace URLS and Images with...  >  (Un) Subscribe thread alerts  
Subject:Demo to replace URLS and Images with...
Summary:Just a demo with Reg. Expressions
Messages:1
Author:Martin Fasani
Date:2009-11-11 14:18:21
 

  1. Demo to replace URLS and Images with...   Reply   Report abuse  
Picture of Martin Fasani Martin Fasani - 2009-11-11 14:18:21
You can see this in action here:
comparasuper.com/es/con_twitter.php


SAMPLE CODE:
<?php
//INCLUDE HERE THIS CLASS
$readTwitter = new TwitterApi;
$readTwitter->Login = 'YourLogin';
$readTwitter->Password = 'YourPass';
$readTwitter->RecivePosts = 15;
$array = unserialize($readTwitter->yoursTweets());
$twit="<ul>";

for($i = 0; $i < sizeof($array); $i++){

//URL: Replace URLs with HTML links or full image paths with <img> tag
$search=array (
'`((http://)(\S+[[:alnum:]]/?))`si');

preg_match($search[0], $array[$i]['msg'], $matches);
if ( substr($matches[0],-4)=='.png' || substr($matches[0],-4)=='.jpg' || substr($matches[0],-4)=='.gif' ) {
$repl =array (
'<img src="$1">');
} else {
$repl =array (
'<a href="$1" rel=\'nofollow\'>$1</a>');
}
$msg=preg_replace($search,$repl, $array[$i]['msg']);

$day= substr($array[$i]['date'],0,10);
$twit.=<<<HTML
<li><img src="asset/twitter.jpg">{$msg} <i>({$day})</i></li><br>
HTML;

}
$twit.="<ul>";


echo $twit;

?>
__________________
Enjoy!