0

Its look like everything ok. It shows the result as well but it also shows Undefined offset: 1 error. Please help me with this.

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";
}
Cù Đức Hiếu
  • 5,569
  • 4
  • 27
  • 35
  • Check `isset` before you access array index. – Manh Nguyen Oct 31 '16 at 10:20
  • Possible duplicate of [undefined offset when using php explode()](http://stackoverflow.com/questions/1807849/undefined-offset-when-using-php-explode) – Gabriel Heming Oct 31 '16 at 10:21
  • Possible duplicate of [PHP Notice: Undefined offset: 1 with array when reading data](http://stackoverflow.com/questions/17456325/php-notice-undefined-offset-1-with-array-when-reading-data) – l'L'l Oct 31 '16 at 10:22
  • But it works ok. If title is not set in a web page, undefined offset can throw – Ima Oct 31 '16 at 10:22

2 Answers2

1

Add some code like bellow:

$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";
    }
}
Razib Al Mamun
  • 2,663
  • 1
  • 16
  • 24
0

I think you have to use preg_match_all with regex simply you code 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;
Fky
  • 2,133
  • 1
  • 15
  • 23