Posted By

laurentzziu on 11/15/10


Tagged

wordpress twitter Followers


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Sporli


Wordpress Twitter Followers


 / Published in: PHP
 

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

Report this snippet  

You need to login to post a comment.