RadGH Posted September 18, 2008 Share Posted September 18, 2008 I'm wondering if its possible to take a string (Such as "/dir/MyFilename.db") and compare it to a wildcard, example: If (isVisible($myFile, '*.db')) { ...show file in list... } Searched google for PHP wildcards, but that isn't helping. The closest thing I found was fnmatch which only works on unix, and isn't what I want anyway (I want to compare strings, not necessarily a filename). I'm trying to build a small file browser and automatically hide what should be hidden, but I could also use the wildcards in other pieces of my program. Link to comment https://forums.phpfreaks.com/topic/124773-compare-string-with-wildcards/ Share on other sites More sharing options...
RadGH Posted September 18, 2008 Author Share Posted September 18, 2008 Looked a bit and came accross the "preg_match" function, but it makes little to no sense (I must be missing something, the documentation doesn't mention all the character it uses in the expressions). Heres what I came up with, although they all return true. Something like this is what I'm looking for, though. test("./Folder/etc"); test("/Folder/etc"); test("/Folder/etc.png"); function test($str) { if (preg_match("/.*/", $str)) { echo "found\n"; } else { echo "not found\n"; } } //Returns "found found found" Link to comment https://forums.phpfreaks.com/topic/124773-compare-string-with-wildcards/#findComment-644547 Share on other sites More sharing options...
thebadbad Posted September 18, 2008 Share Posted September 18, 2008 preg_match() is using regular expressions (good reading here: http://www.regular-expressions.info/), and that is the way you wanna go. If you only want to check if the filename ends in ".db" however, you won't need RegEx, pathinfo() would work for that: <?php function validate($path, $ext) { $info = pathinfo($path); if ($info['extension'] == $ext) { return true; } else { return false; } } if (validate('/dir/MyFilename.db', 'db')) { //do something if file ends in 'db' } ?> If you wanna use more complicated wildcards, preg_match() is good. If you wanna check if a string ends in prefix*.db for example: <?php $path = '/dir/prefix_MyFilename.db'; if (preg_match('~prefix[^/]*\.db$~iD', $path)) { //do something } ?> ~ is a opening pattern delimiter prefix matches literal 'prefix' [^/] matches any character except of a forward slash (/) one time * the asterisk is a quantifier making the preceding char match 0 or more times. \.db matches a literal dot (because it's escaped with backslash - else a dot matches any char) and literal 'db'. $ matches the end of the string, making sure we're dealing with the filename and not e.g. a directory name ~ is a closing pattern delimiter i the i modifier makes the search pattern case-insensitive D the D modifier makes sure the $ only matches 'end of line'. If not specified, the $ also matches just before a new line character at the end. Link to comment https://forums.phpfreaks.com/topic/124773-compare-string-with-wildcards/#findComment-644567 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.