2017-05-18 68 views
0

当PHP别人意想不到的我写的代码使用HTML简单DOM解析器,那就是:foreach循环使用HTML DOM解析器

<?php $opts = array( 'http'=>array( 
    'header'=>"User-Agent:Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53\r\n" )); 
    $context = stream_context_create($opts); 
    set_time_limit(0); 
    include 'simple_html_dom.php'; 
    $lines = file('BQ.txt'); 
    foreach ($lines as $line_num => $line){ 
     $html = file_get_html($line, false, $context); 
     if($html && is_object($html) && isset($html->nodes)){ 
      echo $line; 
     } 
     echo "<ul>"; 
     foreach($html->find("h1[class='productname'], div[class='productprice']") as $element){ 
      if ($element) { } 
      echo "<li>".$element ->plaintext. "</li>"; 
     } else { 
      echo "kosong" 
     } 
     echo "</ul>"; 
    } 
    $html->clear(); ?> 

但它始终解析错误:语法错误,意想不到的“其他'(T_ELSE) 我已经尝试了很多方法,但仍没有得到解决,问题只是 其他{ 回声‘对不起空’ }

如果我运行没有它的脚本,它完美运行

+0

其他部分开始后每个秒结束并移动你的回声内if条件 – JYoThI

回答

0

您的代码中有语法错误。请在下面比较你的代码:

<?php $opts = array( 'http'=>array( 
    'header'=>"User-Agent:Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53\r\n" )); 
    $context = stream_context_create($opts); 
    set_time_limit(0); 
    include 'simple_html_dom.php'; 
    $lines = file('BQ.txt'); 
    foreach ($lines as $line_num => $line){ 
     $html = file_get_html($line, false, $context); 
     if($html && is_object($html) && isset($html->nodes)){ 
      echo $line; 
     } 
     echo "<ul>"; 
     foreach($html->find("h1[class='productname'], div[class='productprice']") as $element){ 
      if ($element) { 
       echo "<li>".$element ->plaintext. "</li>"; 
      } else { 
       echo "kosong" ; 
      } 
     } 
     echo "</ul>"; 
    } 
    $html->clear(); ?> 
+0

对不起,我忘了回声“kosong”; 我已修复它,但仍然无法回声“kosong”只是在结果中回显$行 –

0

Sytax error : Else part started after end of second for each and move your echo inside the if condition

  <?php $foreach ($lines as $line_num => $line){ 
      $html = file_get_html($line, false, $context); 
      if($html && is_object($html) && isset($html->nodes)){ 
       echo $line; 
      } 
      echo "<ul>"; 
      foreach($html->find("h1[class='productname'], div[class='productprice']") as $element){ 
       if ($element) { echo "<li>".$element ->plaintext. "</li>"; } 
       else { 
       echo "kosong" 
       } 
      } 
      echo "</ul>"; 
     } 
    ?> 
+0

echo $ line; } echo“

    ”; foreach($ html-> find(“h1 [class ='productname'],div [class ='productprice']”)as $ element){ if($ element){
  • 。$ element - >明文。 “
  • ”; } else { echo“kosong”; } } echo“
”; } $ html-> clear(); \t?> 结果中仍然没有回声“kosong” –

+0

其他部分只在if语句为假时回显。所以可能永远是真的@FunTasticfantastic – JYoThI

+0

我会检查它.. –

0

主要问题是在这里

if ($element) { } 
     echo "<li>".$element ->plaintext. "</li>"; 
    } else { 
     echo "kosong" 
    } 
    echo "</ul>"; 
} 
$html->clear(); ?> 

解决办法是...

f ($element) { 
     echo "<li>".$element ->plaintext. "</li>"; 
    } else { 
     echo "kosong" ; 
    } 
    echo "</ul>"; 
} 
$html->clear(); 
}?> 

它解析罚款。

+0

对不起,我忘了; 但结果仍然没有回声“kosong” –

+0

回声“kosong”在else块中。因此,如果以前失败,它会起作用。 –

+0

我认为是这样,我的意思是我只想其他只是为第二个foreach但它似乎是整个脚本的一部分 –