调整PHP代码从给定的URL检索DOM


Adjust PHP code Retrieve the DOM from a given URL

你好,

im使用以下代码从URL检索DOM所有"A"标签并打印其HREF现在我的输出包含"A",我不想它的输出在这里http://trend.remal.com/parsing.php

我需要把我的名字写出来http://twitter.com/namehere

因此输出"namehere"的打印列表

    include('simple_html_dom.php');
 // Retrieve the DOM from a given URL
 $html = file_get_html('http://tweepar.com/sa/1/');
 $urls = array();
  foreach ( $html->find('a') as $e )
  {
  // If it's a twitter link
  if ( strpos($e->href, '://twitter.com/') !== false )
  {
    // and we don't have it in the array yet
    if ( ! in_array($urls, $e->href) )
    {
        // add it to our array
        $urls[] = $e->href;
    }
   }
   }
  echo implode('<br>', $urls);
echo $e->href . '<br>';

不要简单地使用$urls[] = $e->href,而是使用正则表达式来匹配用户名:

preg_match('~twitter.com/(.+)~', $e->href, $matches);
$urls[] = $matches[1];