views:

7918

answers:

8

I often find myself doing quick checks like this:

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

But sadly eregi() is going to be deprecated in PHP 6, which means all of my code that uses it will be rendered useless :(.

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

+12  A: 

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.

moff
just to be fussy: stristr is case-insensitive, if you need the case-sensitive ay use strstr
DaNieL
Well, eregi is case-insensitive, too :)
moff
What if $fileName has php in the name but it's not the extension? "example.php.txt" stristr will not work for this.
sirlancelot
You should note that `eregi` uses regular expressions and not just plain text.
Gumbo
@sirlancelot: You're right, but this question was about finding a replacement for eregi (which behaves in the same way as stristr when not using regular expressions), and not to check the file extension of a file :)
moff
@Gumbo: I know that :). Click Upvote didn't want to use regular expressions though, which makes stristr more suitable for this.
moff
@Moff, exactly. This question was about a replacement for eregi(), not finding file extensions :)
Click Upvote
+13  A: 

Of course you are aware, that this doesn't do what you expect it do do? In regexp '.' means any character, so eregi('.php',$fileName) means filename with any character followed by 'php'. Thus for example "blabla2PHP.txt" will match your regexp.

Now what you want to do is this:

$file_ext = pathinfo($filename, PATHINFO_EXTENSION);
if(strtolower($file_ext) != 'php') 
   $filename .= '.php';
vartec
Good point, but this wasn't my question. i used the filename just as an example, what i really want is an alternative function to eregi() which returns true if a String a is found in string b, false otherwise
Click Upvote
+2  A: 

If you go for the "fake" eregi, you shold trigger a notice inside the fake function: trigger_error('Some code still use eregi',E_USER_NOTICE); This way you will easily catch the forgotten eregi calls and can replace them.

Csaba Kétszeri
A: 

In this simple case you should be fine with the string manipulation functions as others have pointed out. However, if you do need regular expressions, "preg" funtions (Pearl compatible) such as preg_replace should be used instead of the "ereg" ones (POSIX).

facildelembrar
+1  A: 

Perhaps you should consider refactoring your code to do this instead:

if (substr($fileName, -4, 4) !== '.php')
    $fileName .= '.php';

As stated in other answers to this question, eregi('.php') will search for anything followed by 'php' ANYWERE in the file (not just at the end).

sirlancelot
+1  A: 

I generally create and endsWith function; or other simple string manipulation functions for this kind of stuff.

function endsWith($string, $end){
    return substr($string, -strlen($end)) == $end;
}
Jasper Bekkers
A: 

stristr i used in install to script file to remove problem and its working....... thanks for this advice.

shyam
Don't add replies as new answers, write comments to the answers that helped you.
dbemerlin
+1  A: 

Try this, I'm using this quite a lot since I updated to PHP 6 recently.

Previously:

if(eregi('-', $_GET['id'])
{
   return true;
}

Now I'm using this - it works just as good.

if(preg_match('/(.+)-(.+)/', $_GET['id'])) {
{
   return true;
}

Just replace your code with the following, and you shouldn't have any hitch of difference within your code. If you're wondering why PHP remove eregi() it's because of the performance issues it has when used a lot, so it's better to use preg_match() as it's more specific in searching so it has better performance and rendering times.

Let me know how this works for you.

WebEntrepreneur