开发者

how to check if file is php?

i want to check if a file has the extensions .php. if it has i include it.

could someone help me with a regexp check?

开发者_开发问答

thanks!


Usually you don't use a regular expression.

The following is a popular method instead:

$extension=pathinfo($filename, PATHINFO_EXTENSION);


pathinfo is the easiest solution, but you can also use fnmatch

if( fnmatch('*.php', $filename) ) { /* do something */ }

EDIT: Like @zombat points out in the comments, if you are after a fast solution, then the following is faster than using pathinfo and fnmatch:

if( substr($filename, -4) === '.php' ) { /* do something */ }

Keep in mind that pathinfo, unlike fnmatch and substr does a basename check on the path you provide, which makes it somewhat cleaner in my opinion.


/\.php$/

but extension mapping doesn't ensure the content is what you expect, just that a file is named a particular way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜