Posted By

ivorpad on 05/24/10


Tagged

get wordpress twitter


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

BrianCoyDesign
laurentzziu


Get the number of Twitter Followers in WordPress


 / Published in: PHP
 

  1. // Get the number of twitter followers
  2.  
  3. function string_getInsertedString($long_string,$short_string,$is_html=false){
  4. if($short_string>=strlen($long_string))return false;
  5. $insertion_length=strlen($long_string)-strlen($short_string);
  6. for($i=0;$i<strlen($short_string);++$i){
  7. if($long_string[$i]!=$short_string[$i])break;
  8. }
  9. $inserted_string=substr($long_string,$i,$insertion_length);
  10. if($is_html && $inserted_string[$insertion_length-1]=='<'){
  11. $inserted_string='<'.substr($inserted_string,0,$insertion_length-1);
  12. }
  13. return $inserted_string;
  14. }
  15.  
  16. function DOMElement_getOuterHTML($document,$element){
  17. $html=$document->saveHTML();
  18. $element->parentNode->removeChild($element);
  19. $html2=$document->saveHTML();
  20. return string_getInsertedString($html,$html2,true);
  21. }
  22.  
  23. function getFollowers($username){
  24. $x = file_get_contents("http://twitter.com/".$username);
  25. $doc = new DomDocument;
  26. @$doc->loadHTML($x);
  27. $ele = $doc->getElementById('follower_count');
  28. $innerHTML=preg_replace('/^<[^>]*>(.*)<[^>]*>$/',"\\1",DOMElement_getOuterHTML($doc,$ele));
  29. return $innerHTML;
  30. }
  31.  
  32.  
  33.  
  34.  
  35. // To display it
  36.  
  37. <?php echo getFollowers("username"); ?>

Report this snippet  

You need to login to post a comment.