2016-10-31 13 views
0

すべてがOKのように見えます。結果も表示されますが、未定義オフセット:1エラー。これで私を助けてください。エラーを取得する:未定義オフセット:1

enter image description here

$url = "http://www.test.com"; 
$pageContent = file_get_contents($url); 
$stepA = explode("</title>",$pageContent); 
$stepB = explode("<title>",$stepA[0]); 
$stepC = $stepB[1]; 
if($stepC == "Not Found"){ 
    echo $stepC = "NA"; 
} else{ 
    echo $stepC = "ok"; 
} 
+0

チェック 'isset'前配列のインデックスにアクセスします。 –

+0

PHPの使用時に[未定義オフセット]の可能な重複可能性があります(http://stackoverflow.com/questions/1807849/undefined-offset-when-using-php-explode) –

+0

[PHPのNotice:Undefined offset: 1配列とデータを読む](http://stackoverflow.com/questions/17456325/php-notice-undefined-offset-1-with-array-when-reading-data) –

答えて

1

怒鳴るようないくつかのコードを追加します。

$url = "http://www.test.com"; 
$pageContent = file_get_contents($url); 

$stepA = explode("</title>",$pageContent); 

if(isset($stepA)) { 
    $stepB = explode("<title>",$stepA[0]); 

    $stepC = isset($stepB) ? $stepB[1] : null; 

    if($stepC == "Not Found"){ 
     echo $stepC = "NA"; 
    } else{ 
     echo $stepC = "ok"; 
    } 
} 
0

私はあなたが正規表現でpreg_match_allを使用する必要があると思うだけで、あなたのコード http://php.net/manual/fr/function.preg-match-all.php

$url = "http://www.test.com"; 
$pageContent = file_get_contents($url); 

preg_match_all("#<title>(.*)<\/title>#sU",$pageContent, $matches); 


if(isset($matches[0][1]) && $matches[0][1] == "Not Found") 
     $stepC = "NA"; 
} else{ 
     $stepC = "ok"; 
} 
echo $stepC; 
関連する問題