PHP 中 eregi() 的好替代品

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/737198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:41:56  来源:igfitidea点击:

Good alternative to eregi() in PHP

phpstring

提问by Click Upvote

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()?我对正则表达式一无所知,也不想学习,所以preg_match()等对我不起作用。

回答by moff

stristrachieves 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 stristrdoesn't accept regular expressions as eregidoes, and for this specific case (checking the extension), you'd better go with vartec's solution.

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

回答by vartec

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

回答by M A Hossain Tonu

Good alternative for eregi()is preg_match()with imodifier:

很好的替代方法eregi()preg_match()使用i修饰符:

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

回答by Bilawal Hameed

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.

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

回答by Csaba Kétszeri

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('Some code still use eregi',E_USER_NOTICE); 这样您就可以轻松地接听被遗忘的 eregi 电话并可以替换它们。

回答by matpie

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 的内容(不仅仅是在最后)。

回答by Jasper Bekkers

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

我一般创建和endsWith函数;或其他用于此类内容的简单字符串操作函数。

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

回答by Charles Zhang

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

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

moff's answser had the parameters backwards.

moff 的回答者的参数倒过来了。

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

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