Good alternative to eregi() in PHP

前端 未结 8 1624
感情败类
感情败类 2021-01-04 08:39

I often find myself doing quick checks like this:

if (!eregi(\'.php\', $fileName)) {
    $filename .= \'.php\';
}

But as eregi() was deprec

8条回答
  •  Happy的楠姐
    2021-01-04 08:53

    stristr achieves exactly the same result as eregi (at least when you don't use regular expressions):

    if (!stristr($fileName, '.php'))
        $filename.='.php';
    

    You could also make a "fake" eregi this way:

    if (!function_exists('eregi')) {
        function eregi($find, $str) {
            return stristr($str, $find);
        }
    }
    

    Update: Note that stristr doesn't accept regular expressions as eregi does, and for this specific case (checking the extension), you'd better go with vartec's solution.

提交回复
热议问题