【发布时间】:2011-12-10 15:15:49
【问题描述】:
我正在从已发布的谷歌电子表格中获取数据,我想要的只是内容 div (<div id="content">...</div>) 中的信息
我知道内容以<div id="content"> 开头,以</div><div id="footer"> 结尾
获取内部 DOM 部分的最佳/最有效方法是什么?我在考虑正则表达式(请参阅下面的示例),但它不起作用,我不确定它是否有效......
header('Content-type: text/plain');
$foo = file_get_contents('https://docs.google.com/spreadsheet/pub?key=0Ahuij-1M3dgvdG8waTB0UWJDT3NsUEdqNVJTWXJNaFE&single=true&gid=0&output=html&ndplr=1');
$start = '<div id="content">';
$end = '<div id="footer">';
$foo = preg_replace("#$start(.*?)$end#",'$1',$foo);
echo $foo;
更新
我想我的另一个问题基本上是关于使用带有起点和终点的正则表达式是否更简单、更容易,而不是尝试解析可能有错误的 DOM,然后提取我需要的部分。似乎正则表达式是可行的方法,但很想听听您的意见。
【问题讨论】:
-
这很有帮助。谢谢。
标签: php regex parsing dom google-docs