PHP file_get_html 的奇怪问题

发布于 2024-10-01 02:49:55 字数 998 浏览 2 评论 0原文

您好,我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梅窗月明清似水 2024-10-08 02:49:55

您确定您的代码没有输入错误吗?

foreach ($snippets as $elemement) {
      echo $urls[$j] . "\n" . $element . "\n\n\n\n";
      $j++;
  }

elementelement 是不同的;你的循环执行得很好,但你的语句可能没有。

Are you sure you did not made a typo in your code?

foreach ($snippets as $elemement) {
      echo $urls[$j] . "\n" . $element . "\n\n\n\n";
      $j++;
  }

element and elemement are different; Your loop executes fine but your statement probably doesn't.

物价感观 2024-10-08 02:49:55

您打错了,$elemenent 实际上应该是 $element

foreach ($snippets as $element) {
    echo $urls[$j] . "\n" . $element . "\n\n\n\n";
    $j++;
}

这是习惯编写可读代码的原因之一。这并不是因为其他人喜欢它,而是因为它使调试变得更加容易。

You made a typo, $elemenent really should be $element.

foreach ($snippets as $element) {
    echo $urls[$j] . "\n" . $element . "\n\n\n\n";
    $j++;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文