使用 PHP 从特定的 div id 获取数据 [英] Fetching Data From A Specific div id Using PHP

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

问题描述

可能重复:
从php读取XML标签id

使用 PHP 从特定的 div id 获取数据的方法是什么.我想要做的是从一个名为 <div id="content"> 的 div id 中获取数据,因此该 div id 中的所有数据都将在一个变量中获取.
我可以使用我的脚本获取所有内容,但无法对其进行过滤以从特定 div 标签中获取数据.
这是我用来获取任何内容的脚本:

what is the way to fetch data from A Specific div id Using PHP. What i want to do is to fetch data from a div id called <div id="content"> , so all the data from that div id will be fetched in a variable.
I can fetch all content with my script but cant filter it to fetch data from a Specific div tag.
Here is the script i am using to fetch any content:

function file_get_contents_curl($url)
{
$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}
$html = file_get_contents_curl("http://www.example.com");
//parsing all content:
$doc = new DOMDocument();
@$doc->loadHTML($html);
echo "$html";


有什么想法吗?


any idea?

推荐答案

试试这个,但要确保你已经下载并包含 PHP 简单 HTML DOM 解析器

Try this but make sure you have downloaded and included PHP Simple HTML DOM Parser

$html = file_get_html("http://www.example.com");
$displaybody = $html->find('div[id=content]', 0)->plaintext;

它们是一些从 div id 或 Tag id 中排除内容的方法,例如,

Their are some ways to exclude content from div id or Tag id like,

1) 使用正则表达式

2) 使用 SimpleXML

3) 使用 DOM 扩展XPath

这篇关于使用 PHP 从特定的 div id 获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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