获取错误:未定义的偏移量:1

时间:2016-10-31 10:15:58

标签: php explode

看起来一切都好。它也显示了结果,但它也显示未定义的偏移: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";
}

2 个答案:

答案 0 :(得分:1)

添加一些代码如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";
    }
}

答案 1 :(得分:0)

我认为你必须使用preg_match_all和regex只需编码 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;