0

我写了一个代码来使用 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(); ?>

但它总是解析错误:语法错误,意外'else'(T_ELSE)我尝试了很多方法但仍然没有解决方案,问题只是else{ echo "sorry empty" }

如果我在没有它的情况下运行脚本,它会完美运行

4

3 回答 3

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();
}?>

它解析得很好。

于 2017-05-18T06:57:45.480 回答
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(); ?>
于 2017-05-18T06:50:56.930 回答
0

语法错误:其他部分在每个秒结束后开始,并将您的回声移动到 if 条件内

          <?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>"; 
         }
    ?>
于 2017-05-18T06:51:05.013 回答