简体   繁体   中英

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$/

但扩展映射并不能确保内容符合您的期望,只是文件以特定方式命名。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM