12

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.

NikiC
  • 100,734
  • 37
  • 191
  • 225
Ali
  • 261,656
  • 265
  • 575
  • 769
  • 1
    I hope you realize that `eregi()` IS a regular expression function. POSIX-style. `preg_match()` is a regular expression function, Perl-style. – matpie Apr 10 '09 at 17:14
  • You might find [`s($str)->endsWith('.php')`](https://github.com/delight-im/PHP-Str/blob/8fd0c608d5496d43adaa899642c1cce047e076dc/src/Str.php#L117) and all the other string functions helpful, as found in [this standalone library](https://github.com/delight-im/PHP-Str). – caw Jul 27 '16 at 03:08

8 Answers8

24

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
  • 6,415
  • 31
  • 30
  • just to be fussy: stristr is case-insensitive, if you need the case-sensitive ay use strstr – Strae Apr 10 '09 at 10:29
  • 2
    Well, eregi is case-insensitive, too :) – moff Apr 10 '09 at 11:40
  • What if $fileName has php in the name but it's not the extension? "example.php.txt" stristr will not work for this. – matpie Apr 10 '09 at 17:21
  • You should note that `eregi` uses regular expressions and not just plain text. – Gumbo Apr 10 '09 at 17:30
  • @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 Apr 10 '09 at 17:32
  • @Gumbo: I know that :). Click Upvote didn't want to use regular expressions though, which makes stristr more suitable for this. – moff Apr 10 '09 at 17:37
  • @Moff, exactly. This question was about a replacement for eregi(), not finding file extensions :) – Ali Apr 11 '09 at 09:59
19

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
  • 131,205
  • 36
  • 218
  • 244
  • 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 – Ali Apr 11 '09 at 09:57
5

Good alternative for eregi() is preg_match() with i modifier:

if (! preg_match('/.php/i',$fileName))
      $filename.='.php';
M A Hossain Tonu
  • 1,437
  • 15
  • 14
4

Try this, I'm using this quite a lot since I updated to PHP 5 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.

Bilawal Hameed
  • 258
  • 4
  • 11
2

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
  • 674
  • 3
  • 6
2

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).

matpie
  • 17,033
  • 9
  • 61
  • 82
1

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
  • 6,711
  • 32
  • 46
0

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

moff's answser had the parameters backwards.

http://php.net/manual/en/function.stristr.php

Charles Zhang
  • 243
  • 2
  • 9