PHP中eregi()的良好替代品

时间:2022-09-22 21:00:53

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.

但是由于eregi()在PHP 5.3中被弃用,现在代码会抛出错误。

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.

是否有另一个函数的行为与eregi()完全相同?我对regexp一无所知,也不想学习,所以preg_match()等对我不起作用。

8 个解决方案

#1


23  

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

stristr实现与eregi完全相同的结果(至少在不使用正则表达式时):

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

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

你也可以这样做一个“假的”eregi:

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.

更新:请注意,stristr不像eregi那样接受正则表达式,对于这种特定情况(检查扩展名),最好使用vartec的解决方案。

#2


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.

当然你知道,这不符合你的预期吗?在正则表达式'。'表示任何字符,因此eregi('。php',$ fileName)表示带有任何字符后跟'php'的文件名。因此,例如“blabla2PHP.txt”将匹配您的正则表达式。

Now what you want to do is this:

现在你要做的是:

$file_ext = pathinfo($filename, PATHINFO_EXTENSION);
if(strtolower($file_ext) != 'php') 
   $filename .= '.php';

#3


5  

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

eregi()的好替代方法是带有i修饰符的preg_match():

if (! preg_match('/.php/i',$fileName))
      $filename.='.php';

#4


4  

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

试试这个,自从我最近更新到PHP 5以来,我使用了这么多。

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.

只需用以下内容替换您的代码,您的代码就不应该有任何不同之处。如果你想知道PHP删除eregi()的原因是因为它在使用时遇到了很多性能问题,所以最好使用preg_match(),因为它在搜索时更具体,因此它具有更好的性能和渲染时间。

Let me know how this works for you.

让我知道这对你有什么用。

#5


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.

如果你选择“假”eregi,你会在假函数内触发一个通知:trigger_error('某些代码仍使用eregi',E_USER_NOTICE);通过这种方式,您可以轻松捕获被遗忘的eregi电话,并可以替换它们。

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

正如在这个问题的其他答案中所述,eregi('。php')将在文件中搜索任何后跟“php”ANYWERE的内容(不仅仅是在结尾处)。

#7


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;
}

#8


0  

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

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

moff's answser had the parameters backwards.

莫夫的回答者有倒退的参数。

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

#1


23  

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

stristr实现与eregi完全相同的结果(至少在不使用正则表达式时):

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

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

你也可以这样做一个“假的”eregi:

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.

更新:请注意,stristr不像eregi那样接受正则表达式,对于这种特定情况(检查扩展名),最好使用vartec的解决方案。

#2


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.

当然你知道,这不符合你的预期吗?在正则表达式'。'表示任何字符,因此eregi('。php',$ fileName)表示带有任何字符后跟'php'的文件名。因此,例如“blabla2PHP.txt”将匹配您的正则表达式。

Now what you want to do is this:

现在你要做的是:

$file_ext = pathinfo($filename, PATHINFO_EXTENSION);
if(strtolower($file_ext) != 'php') 
   $filename .= '.php';

#3


5  

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

eregi()的好替代方法是带有i修饰符的preg_match():

if (! preg_match('/.php/i',$fileName))
      $filename.='.php';

#4


4  

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

试试这个,自从我最近更新到PHP 5以来,我使用了这么多。

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.

只需用以下内容替换您的代码,您的代码就不应该有任何不同之处。如果你想知道PHP删除eregi()的原因是因为它在使用时遇到了很多性能问题,所以最好使用preg_match(),因为它在搜索时更具体,因此它具有更好的性能和渲染时间。

Let me know how this works for you.

让我知道这对你有什么用。

#5


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.

如果你选择“假”eregi,你会在假函数内触发一个通知:trigger_error('某些代码仍使用eregi',E_USER_NOTICE);通过这种方式,您可以轻松捕获被遗忘的eregi电话,并可以替换它们。

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

正如在这个问题的其他答案中所述,eregi('。php')将在文件中搜索任何后跟“php”ANYWERE的内容(不仅仅是在结尾处)。

#7


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;
}

#8


0  

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

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

moff's answser had the parameters backwards.

莫夫的回答者有倒退的参数。

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