2013-04-20 55 views
-1

当在字符串中找到“最佳匹配”时,下面的脚本应该结束,但即使我知道它最终被发现脚本仍在运行。请帮我解决我的错误。请帮我修复这个PHP,而声明

$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); 



} 
+0

解决您的缩进,你会看到这个问题。 – 2013-04-20 03:01:20

+0

为什么'$ end'是一个既可以是“1”又可以是“2”的字符串,并与整数进行比较?为什么不把它做成一个布尔值?为什么不使用'break 2'?如果'strpos'的结果是'0',它不会与'true'相比(反正毫无意义的比较)。 – Ryan 2013-04-20 03:01:50

回答

0

在你strpos,你与真正比较,这是不对的。
此外,在临如果声明你应该打破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/>"; 
     } 
     } 
    } 
    } 
} 
0

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

if (strpos($text, "best match") == true) { 
    $end = 2; 
    break; # Terminate execution of foreach loop 
} 
+0

不,这不起作用): – user1973004 2013-04-20 03:21:50

+0

@ scoota269。我认为它应该是'break 2';'从foreach'和'while'退出 – Amir 2013-04-20 04:00:38