A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearGood alternative to eregi() in PHP - page 1
User InfoPosts
Good alternative to eregi() in PHP#1
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.

posted date: 2009-04-10 01:56:00

Re: Good alternative to eregi() in PHP#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-10 01:56:01

Re: Good alternative to eregi() in PHP#3
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.

posted date: 2009-04-10 02:02:00

Re: Good alternative to eregi() in PHP#4
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(;

posted date: 2009-04-10 02:14:00

Re: Good alternative to eregi() in PHP#5
just to be fussy: stristr is case-insensitive, if you need the case-sensitive ay use strstr

posted date: 2009-04-10 02:29:00

Re: Good alternative to eregi() in PHP#6
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.

posted date: 2009-04-10 03:11:00

Re: Good alternative to eregi() in PHP#7
Well, eregi is case-insensitive, too :)

posted date: 2009-04-10 03:40:00

Re: Good alternative to eregi() in PHP#8
I hope you realize that eregi() IS a regular expression function. POSIX-style. preg_match() is a regular expression function, Perl-style.

posted date: 2009-04-10 09:14:00

Re: Good alternative to eregi() in PHP#9
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).

posted date: 2009-04-10 09:17:00

Re: Good alternative to eregi() in PHP#10
What if $fileName has php in the name but it's not the extension? "example.php.txt" stristr will not work for this.

posted date: 2009-04-10 09:21:00

Re: Good alternative to eregi() in PHP#11
You should note that eregi uses regular expressions and not just plain text.

posted date: 2009-04-10 09:30:00

Re: Good alternative to eregi() in PHP#12
@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 :)

posted date: 2009-04-10 09:32:00

select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.