开发者

PHP script that reads external HTML source code and lists the code between the tags

开发者 https://www.devze.com 2023-01-06 08:04 出处:网络
Basically I want to write php code that lists all the contents that are between <h1> tags from external url.

Basically I want to write php code that lists all the contents that are between <h1> tags from external url.

I don't want just the first but all of them. So if the source of the external website is

<html>
  <title></title>
  <head></head>
  <h1>Test Here</h1>
  <h1>Test here</h1>
</html>

I want to make a script that generates only the content between the <h1> tags that would be开发者_如何学运维:

Test Here
Test here

I'm familiar with PHP but I just cant think of scripts that do that.


simple_html_dom is your friend.

$dom = file_get_html("http://yourserver.com/path/to/file.html");
// alternatively use str_get_html($html) if you have the html string already...

foreach ($dom->find("h1") as $node)
{
    echo $node->innertext;
}

It is very powerful and can do much, much more.

0

精彩评论

暂无评论...
验证码 换一张
取 消