Jump to content



Photo

Twitter Widget in PHP


  • Please log in to reply
5 replies to this topic

#1 Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 09 June 2012 - 13:37

So, I've successfully gotten a Twitter widget to work, but there's a problem with the preg_replace on the first $ret. These variables are obviously created to find links and hash tags. The first one was meant to detect http:// links and the second one is for www and ftp.

If you test it out and hover your mouse over a link on the latest tweet, you'll notice that there's "http//" instead of "http://".

Can I get a little help on that please? :)

<?php
function getLastXTwitterStatus($userid,$x){
$url = "http://twitter.com/statuses/user_timeline/$userid.xml?count=$x&quot";
$xml = simplexml_load_file($url) or die('could not connect');
echo '<ul>';
	   foreach($xml->status as $status){
	   $text = twitterify( $status->text );
	echo '<li>'.utf8_decode($text).'</li>';
	   }
	echo '</ul>';
}
function twitterify($ret) {
  $ret = preg_replace("#(^|[\n ])([\w]+?<img src='http://www.neowin.net/forum/public/style_emoticons/<#EMO_DIR#>/hmm.gif' class='bbc_emoticon' alt=':/' />/[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"http://\\2\" >\\2</a>", $ret);
  $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" >\\2</a>", $ret);
  $ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" >@\\1</a>", $ret);
  $ret = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" >#\\1</a>", $ret);
return $ret;
}
getLastXTwitterStatus('IllingSpree',2);
?>



#2 CrispCreations

CrispCreations

    Neowinian

  • Joined: 19-April 04
  • Location: UK

Posted 10 June 2012 - 10:19

Your first regex is a bit borked, try this one instead (I use it myself and have done from some time in a Twitter app I wrote)...

    $ret = preg_replace('@(https?<img src='http://www.neowin.net/forum/public/style_emoticons/<#EMO_DIR#>/hmm.gif' class='bbc_emoticon' alt=':/' />/([-\w\.]+)+(/([\w/_\-\.]*(\?\S+)?(#\S+)?)?)?)@','<a href="$1">$1</a>',$ret);


#3 OP Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 13 June 2012 - 12:43

Appreciate it broski!

Is there an alternative to the "or die" method? Because we all know it kills all the code that comes after it.

$xml = simplexml_load_file($url) or die('could not connect');


#4 CrispCreations

CrispCreations

    Neowinian

  • Joined: 19-April 04
  • Location: UK

Posted 13 June 2012 - 14:53

Appreciate it broski!

Is there an alternative to the "or die" method? Because we all know it kills all the code that comes after it.

$xml = simplexml_load_file($url) or die('could not connect');

NP mate.

If it can't connect the code after it can't continue anyway, if you want to do something else other than die just code it in an if / else block statement...


if ($xml = simplexml_load_file($url)) {
    // existing code here ....
} else {
    // handle failure here ...
}


#5 OP Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 13 June 2012 - 15:26

I should of known the if statement would've been the best method for the fact that I'm always looking at if's on WordPress' template. :huh:

Works flawlessly of course. :)

Thanks again! :D

#6 OP Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 21 March 2013 - 02:04

Anyone know a preg_replace to get the image links from instagram and twitter?