Good alternative to eregi() in PHP

前端 未结 8 1617
感情败类
感情败类 2021-01-04 08:39

I often find myself doing quick checks like this:

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

But as eregi() was deprec

8条回答
  •  失恋的感觉
    2021-01-04 09:00

    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.

提交回复
热议问题