-1

当在字符串中找到“最佳匹配”时,下面的脚本应该结束,但即使我知道最终找到它,脚本也会继续运行。请帮我解决我的错误。

$end = "1";
while ($end != 2) {
foreach($anchors as $a) { 
    $i = $i + 1;
    $text = $a->nodeValue;
    $href = $a->getAttribute('href');


        //if ($i<80) {
    //if (strpos($item, ".$array.") == false) {


    //}
      if (strpos($text, "best match") == true) {
$end = "2";
}
   if (strpos($text, "by owner") === false) {
       if (strpos($text, "map") === false) {
   if ($i > 17) {

     echo "<a href =' ".$href." '>".$text."</a><br/>";

}
   }

   }

    }
        //$str = file_get_contents($href);
//$result = (substr_count(strip_tags($str),"ipod"));
//echo ($result);



}
4

2 回答 2

0

问题是嵌套循环。当您找到“最佳匹配”时,您还需要结束 foreach 循环。尝试:

if (strpos($text, "best match") == true) {
    $end = 2; 
    break; # Terminate execution of foreach loop
}
于 2013-04-20T03:01:44.007 回答
0

在 yourstrpos中,您正在与true进行比较,这是错误的。
此外,在if语句中,您应该中断 foreach 和 while 循环。

这是正确的代码:

<?php

while ($end != 2) {

  foreach($anchors as $a) {
    $text = $a->nodeValue;
    $href = $a->getAttribute('href');

    if (strpos($text, "best match") !== false) {
      $end = "2"; 
      break 2;
    }

    if (strpos($text, "by owner") === false) {
      if (strpos($text, "map") === false) {
        if ($i > 17) {
          echo "<a href =' ".$href." '>".$text."</a><br/>";
        }
      }
    }
  }
}
于 2013-04-20T04:02:25.990 回答