![](/img/trans.png)
[英]How do I wait for my script to load the page with PHP Simple HTML DOM Parser?
[英]How do I filter my results ? Html Dom Parser
我得到以下代码:
<?php
include('simple_html_dom.php');
$html = file_get_html('http://www.google.com/search?q=BA236',false);
$e = $html->find("div[class=g]");
echo $e[0]->innertext;
?>
当我运行它时,我得到了谷歌搜索结果的第一类,它是:
British Airways Flight 236
Scheduled departs in 13 hours 13 mins
Departure DME 5:40 AM —
Moscow Dec 15
Arrival LHR 6:55 AM Terminal 5
London Dec 15
Scheduled departs in 1 day 13 hours
Departure DME 5:40 AM —
Moscow Dec 16
Arrival LHR 6:55 AM Terminal 5
London Dec 16
我的问题是我不需要所有这些信息,而且我不知道如何过滤此回声,因为HTML代码没有id或类。 我考虑过用jQuery或简单的CSS隐藏不需要的html,但是:同样的问题,我没有id或类来调用它们。
因此,我该如何过滤掉我不想要的信息。 请给我看一个例子,我将检查需要删除自己的html。 谢谢。
您要搜索的内容称为grep
工具(或正则表达式)。 请参阅SO网站的PHP,以在txt文件中进行搜索,并在整行中回显可能的答案。 稍微修改到您的应用程序:
$contents = 'British Airways Flight 236\n\nScheduled departs in 13 hours 13 mins\n\nDeparture DME 5:40 AM —\n\Moscow Dec 15\n\n...'
$searchfor = 'departs';
$pattern = preg_quote($searchfor, '/');
// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*\$/m";
// search, and store all matching occurences in $matches
if (preg_match_all($pattern, $contents, $matches)) {
echo "Found matches:\n";
echo implode("\n", $matches[0]);
} else {
echo "No matches found";
}
编辑:
或者,如注释中所述,使用->saveHTML
而不是->innertext
保留HTML结构,以便于解析。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.