Good alternative to eregi() in PHP

前端 未结 8 1585
感情败类
感情败类 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:03

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

提交回复
热议问题