私はこのコードを使用しています:
<?php
include_once('/simple_html_dom.php');
$dom = file_get_html("http://www.gsmarena.com/samsung_i9300_galaxy_s_iii-4238.php");
foreach ($dom->find('tr') as $node) {
if (is_a($node->children(0), 'simple_html_dom_node')) {
if ($node->children(0)->plaintext == "Protection") {
$plain = explode(',', $node->children(1)->plaintext);
if($plain[0] === null){echo 'Nu are';}
elseif($plain[0]!== null) {echo $plain[0];
if($plain[1] === null){echo 'Nu are';}
elseif($plain[1] !== null) {echo $plain[1];}
}
}
}
}
?>
私が望むのは、 $plain[x] または $plain[y] が存在するかどうかだけで、カスタムテキストを表示しないかどうかを示します(私の場合は「Nu are」)。現在のケースでは $plain[1] が存在しないため、コードは正常に機能しますが、エラーも発生します。
コードから受け取った結果は次のとおりです。
Corning Gorilla Glass 2
Notice: Undefined offset: 1 in E:\server\htdocs\preluare\1.php on line 10
Nu are
最初はオフセット0を示しています.existと2番目のオフセットが存在しないため、エラーが表示され、エラーの後が正しいカスタムテキストです。
正しい結果はCorning Gorilla Glass 2
Nu are