PHP-从页面中检索内容

问题描述:

我想检索页面的内容并根据我的喜好重新格式化...

I'd like to retrieve a page's content and reformat it to my liking...

例如:

  • 转到example.com
  • 获取类别为x"的标签内的内容
  • 将内容传递给特定变量
  • 以某种漂亮的形式吐出内容..array、csv、xml...

不会太难吧?我是 PHP 菜鸟!:)

Not too hard, right? I'm a PHP noob! :)

尝试使用 PHP Simple HTML DOM Parser.

你可以做这样的好事:

// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');

// Find all images
foreach($html->find('img') as $element)
       echo $element->src . '<br>';

// Find all links with class=x
foreach($html->find('a[class=x]') as $element)
       echo $element->href . '<br>';