PHP从第x页获取div

时间:2013-03-26 17:32:29

标签: php file-get-contents

我想从第Y页中提取一些html

for example site is
<head>xxxx</head>
<body>....<div id="ineedthis_code"> </div> ...</body>

可以执行此file_get_contents吗? 我只需要那个div

1 个答案:

答案 0 :(得分:1)

不使用特殊库(在大多数情况下这是最好的方法),您可以使用explode-function:

$content = file_get_contents($url);
$first_step = explode( '<div id="YOUR ID HERE">' , $content ); // So you will get two array elements

$second_step = explode("</div>" , $first_step[1] ); // "1" depends, if you have more elements with this id (theoretical) 

echo $second_step[0]; // You will get the first element with the content within the DIV :)

请注意,这只是一个没有错误处理的例子。它也适用于特殊情况;如果html结构没有改变。即使是简单的空格也会破坏这段代码所以你应该更好地使用解析库;-)