Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good alternative to eregi() in PHP

Tags:

string

php

I often find myself doing quick checks like this:

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

But as eregi() was deprecated in PHP 5.3 the code now throws errors.

Is there another function that behaves exactly the same way as eregi()? I don't know anything about regexps and don't want to learn, so preg_match() etc won't work for me.

like image 961
Ali Avatar asked Apr 10 '09 09:04

Ali


1 Answers

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.

like image 118
moff Avatar answered Sep 23 '22 15:09

moff