PHP从第x页获取一个div [英] PHP get a div from page x

查看:51
本文介绍了PHP从第x页获取一个div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Y页提取一些HTML

I wanna extract some html from page Y

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

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

it is possible to do this file_get_contents ?! i need only that div nothing else

推荐答案

无需使用特殊的库(在大多数情况下,这是最好的方法),则可以使用explode函数:

Without using a special library (which is the best way in most cases), you can use the 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结构不发生变化,则不会.即使是简单的空格也会破坏此代码.因此,您最好使用解析库;-)

Please note, it's only an example without error handling. It also works onlny on a special case; not if the html structure ist changing. Even simple spaces can break this code. So you should better use a parsing library ;-)

这篇关于PHP从第x页获取一个div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆