strpos空分隔符

时间:2012-05-15 01:53:51

标签: php arrays strpos

function findit($gbq, $kwords){

$original_file = file_get_contents($gbq);

$keytar = $kwords;
$ckeytar = strtoupper($keytar);
$okeytar = strtolower($keytar);

$arrit = array(
    "original" => $keytar,
    "ocap" => $ckeytar,
    "olow" => $okeytar,
);

if(strpos($original_file, $arrit['original']) == true) {
    $fp = fopen("linkter.html", 'a');
    fwrite($fp, "<a href='" . $gbq . "' style='color:orange;'>" . $gbq . "</a>");
    fclose($fp);
}
elseif(strpos($original_file, $arrit['ocap']) == true) {
    $fp = fopen("linkter.html", 'a');
    fwrite($fp, "<a href='" . $gbq . "' style='color:red;'>" . $gbq . "</a>");
    fclose($fp);
}
elseif(strpos($original_file, $arrit['olow']) == true) {
    $fp = fopen("linkter.html", 'a');
    fwrite($fp, "<a href='" . $gbq . "' style='color:green;'>" . $gbq . "</a>");
    fclose($fp);
}
else{
    echo "String not found";
}

}

由于某种原因,它说第二个条件($ arrit ['ocap'])中有一个空分隔符。

我已经阅读了其他一些答案,但我找不到一个有效的(或者我觉得可行)

1 个答案:

答案 0 :(得分:0)

使用stripos()而不是strpos()简化操作时会发生什么情况,如下所示:

function findit($gbq, $kwords)
{
    $original_file = file_get_contents($gbq);

    if (stripos($original_file, $kwords) !== false)
    {
        $fp = fopen("linkter.html", 'a');
        fwrite($fp, "<a href='" . $gbq . "' style='color:orange;'>" . $gbq . "</a>");
        fclose($fp);
    }
    else
    {
        echo "String not found";
    }
}