使用HTMLDOM解析器时,php中的foreach循环和意外else
我已经编写了使用html简单dom解析器的代码,如下所示:使用HTMLDOM解析器时,php中的foreach循环和意外else,php,html,xpath,simpledom,Php,Html,Xpath,Simpledom,我已经编写了使用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"
<?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(); ?>
您的代码中有语法错误。请将您的代码与以下代码进行比较:
<?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(); ?>
Sytax错误:Else部分在每秒钟结束后开始,并在if条件内移动您的回声
主要问题在这里
if ($element) { }
echo "<li>".$element ->plaintext. "</li>";
} else {
echo "kosong"
}
echo "</ul>";
}
$html->clear(); ?>
if($element){}
回显“”$element->纯文本。“ ”;
}否则{
回声“歌颂”
}
回声“”;
}
$html->clear();?>
解决办法是
f ($element) {
echo "<li>".$element ->plaintext. "</li>";
} else {
echo "kosong" ;
}
echo "</ul>";
}
$html->clear();
}?>
f($element){
回显“”$element->纯文本“ ”;
}否则{
呼应“歌颂”;
}
回声“”;
}
$html->clear();
}?>
它解析很好。Else部分在每秒钟结束后开始,并将您的回音移动到if条件中对不起,我忘记了回音“kosong”;我已经把它修好了,但仍然不能回显“kosong”,只是回显了$行,结果我忘了;但结果中仍然没有回声“kosong”,因为“kosong”在else块中。我想是的,我的意思是我只想对第二个foreach使用else,但它似乎是整个scriptforeach($html->find(“h1[class='productname'],div[class='productprice'])as$element){//if-else块}的一部分。在这里,您可以放置第二个if-else块。然后它必须是第二个foreach循环的一部分。现在请明确一点,在您的代码中有两个foreach循环,它们是嵌套的还是分离的??我希望它是分离的detecho$line;}回声“”;foreach($html->find($h1[class='productname'],div[class='productprice'])作为$element){if($element){echo“- ”。$element->纯文本。
”;}否则{echo“kosong”;}}echo“
”;}$html->clear();?>ResulteSe部分中仍然没有回显“kosong”,只有当if语句为false时才会回显。所以可能永远是真的@FuntasticFantastic因为第一个foreach是真的,所以else不会回音,这可能是问题吗?可能是如果($element)总是真的,那么else部分不会回音,我的意思是只有第二个foreach的“else”,但它似乎是整个脚本的一部分
f ($element) {
echo "<li>".$element ->plaintext. "</li>";
} else {
echo "kosong" ;
}
echo "</ul>";
}
$html->clear();
}?>