I use the following code to extract some usernames from Twitter. What I done so far is to
get like these:
[0] => com/USERNAME/statuses/167362593990778881USERNAME@twitter.
[1] => com/ANOTHER_USERNAME/statuses/167362593390997506ANOTHER_USERNAME@twitter.
this is my code.. how can I extract only the usernames?
$file = file_get_contents("http://search.twitter.com/search.rss?q=twitter");
$file = strip_tags($file);
preg_match_all("([a-z0-9!#$%&'*+/=?^_`{|}~-]*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)\b)siU", $file, $matches);
echo '<pre>';
print_r($matches);
echo '</pre>';
I did this using simplexml but I get only the first result
$url="http://search.twitter.com/search.atom?q=hello";
$twitter_xml = simplexml_load_file($url);
foreach ($twitter_xml->entry->author as $key) {
$author = $key->{"uri"};
echo"<li><h5>$author</h5></li>";
}
Stop doing that. Using regex when you have multiple properly structured, machine-readable formats available to you is silly.
You can use SimpleXML to parse an RSS feed and pull out the elements you need, or you can use the even easier to work with JSON representation (http://search.twitter.com/search.json?q=twitter) and run it through
json_decodeto get a nice PHP array of objects, with all the data you’re looking to extract already broken out for you.