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.
精彩评论