how to implement dos match pattern by php
* represent anything
? represent a character
like dos filename
*.test.com
will match aaa.test.com
but not match test.com
a dos like regexp *.test.com
is similar with regexp /.*\.test\.com/
in php
is there a builtin function to test dos-like re开发者_开发问答gexp in php?
As far as I can see in your case glob()
and/or fnmatch()
will do it.
$pattern = '*.test.com';
var_dump(fnmatch($pattern, 'aaa.test.com'));
var_dump(fnmatch($pattern, 'test.com'));
Note, that this are not regular expressions, because this is just a much more simple pattern matching. I don't see a reason, why you should use regex here, thus at least fnmatch()
should do it more efficient.
精彩评论