修改PHP代码以从文档中提取特定字符串

Ene*_*oma -1 php regex

我使用以下代码从Twitter中提取一些用户名.到目前为止我做的是这样:

    [0] => com/USERNAME/statuses/167362593990778881USERNAME@twitter.
    [1] => com/ANOTHER_USERNAME/statuses/167362593390997506ANOTHER_USERNAME@twitter.
Run Code Online (Sandbox Code Playgroud)

这是我的代码..我怎么才能只提取用户名?

    $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>';
Run Code Online (Sandbox Code Playgroud)

我使用simplexml做了这个,但我得到的只是第一个结果

$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>";
}
Run Code Online (Sandbox Code Playgroud)

cee*_*yoz 5

别那样做.当你有多种结构合理,机器可读的格式时,使用正则表达式是愚蠢的.

您可以使用SimpleXML来解析RSS提要并提取所需的元素,或者您可以更轻松地使用JSON表示(http://search.twitter.com/search.json?q=twitter)并运行它通过json_decode获得一个很好的PHP对象数组,所有你想要提取的数据已经为你解决了.