PHP file_get_html 的奇怪问题
您好,我正在尝试使用 simple_html_dom 进行文本(网站)集群项目,但我遇到了一个奇怪的问题。当我在外循环内使用 echo 时,url 和片段是您所期望的,但是当我尝试回显我在循环外收集的数组内容时,url 没问题,但片段消失了,最后一个片段位于其位置。
<?php
// create HTML DOM
include("simple_html_dom.php");
$search_query = 'something';
$j = 1;
$k = 1;
/*************************GOOGLE***************************/
for ($i = 0; $i < 1; $i++) {
$url = sprintf('http://www.google.com/search?q=%s&start=%d', $search_query, 10 * $i);
$html = file_get_html($url);
foreach ($html->find('a[class=l]') as $element) {
$urls[$j] = $element->href;
echo $element->href . "\n\n\n\n\n";
$j++;
}
foreach ($html->find('div[class=s]') as $element) {
$snippets[$k] = $element->innertext;
echo $element->innertext . "\n\n\n\n\n";
$k++;
}
}
$j = 1;
foreach ($snippets as $elemement) {
echo $urls[$j] . "\n" . $element . "\n\n\n\n";
$j++;
}
?>
Hi i am trying to use simple_html_dom for a text(website) clustering project but i have run into a weird problem. When i use echo inside the outer loop the url and the snippet are what you would expect but when i try to echo the array contents i have gathered outside the loop the urls are ok but the snippets are gone and the last snippet is in their place.
<?php
// create HTML DOM
include("simple_html_dom.php");
$search_query = 'something';
$j = 1;
$k = 1;
/*************************GOOGLE***************************/
for ($i = 0; $i < 1; $i++) {
$url = sprintf('http://www.google.com/search?q=%s&start=%d', $search_query, 10 * $i);
$html = file_get_html($url);
foreach ($html->find('a[class=l]') as $element) {
$urls[$j] = $element->href;
echo $element->href . "\n\n\n\n\n";
$j++;
}
foreach ($html->find('div[class=s]') as $element) {
$snippets[$k] = $element->innertext;
echo $element->innertext . "\n\n\n\n\n";
$k++;
}
}
$j = 1;
foreach ($snippets as $elemement) {
echo $urls[$j] . "\n" . $element . "\n\n\n\n";
$j++;
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定您的代码没有输入错误吗?
element
和element
是不同的;你的循环执行得很好,但你的语句可能没有。Are you sure you did not made a typo in your code?
element
andelemement
are different; Your loop executes fine but your statement probably doesn't.您打错了,
$elemenent
实际上应该是$element
。这是习惯编写可读代码的原因之一。这并不是因为其他人喜欢它,而是因为它使调试变得更加容易。
You made a typo,
$elemenent
really should be$element
.This is one reason to get used to make readable code. It's not because others like it, but because it makes debugging much easier.