TEENFRONT Posted September 7, 2008 Share Posted September 7, 2008 Hey im usuing preg_match() to try match a search term to a file title in a directory. Now i have the directory listing script working fine, it basically lists all files in a directory, simple. But i tried making it only list files matching a search term. // $filetitle is something like 'Transformers' // $search is 'Transformers' if(preg_match('/$search/', $filetitle)) { echo "$filetitle Contains '$search'<br />"; // success! } else { echo "$filetitle - Doesn't contain '$search' <br />"; // Doesnt match, not good. } as a test im getting it to loop through all the files in the directory and say if it containts the search term or not. All i get in the output is Transformers - Doesn't contain 'Transformers' otherfilm - Doesn't contain 'Transformers' otherfilm - Doesn't contain 'Transformers' etc etc Actually...i may have just figured out my issue... preg_match uses $ as an end sign for the search right..so basically its matching nothing...hmm.. in that case, new questions... Whats the best way to search the file names and try matching them to a search term as if i cannot use a variable in preg_match...? Link to comment https://forums.phpfreaks.com/topic/123134-preg_match-ahhhhhhhhhhhhh/ Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 You need to use double quotes for variables to parse in. <?php $regex = "/$search/i"; if (preg_match($regex, $filetitle)) { //etc Link to comment https://forums.phpfreaks.com/topic/123134-preg_match-ahhhhhhhhhhhhh/#findComment-635885 Share on other sites More sharing options...
TEENFRONT Posted September 7, 2008 Author Share Posted September 7, 2008 i ended up with this.. if(fnmatch("/$search/", $filetitle)) { this works... whats the difference with preg and fn? Link to comment https://forums.phpfreaks.com/topic/123134-preg_match-ahhhhhhhhhhhhh/#findComment-635892 Share on other sites More sharing options...
Ken2k7 Posted September 7, 2008 Share Posted September 7, 2008 Well fn is short for file name (I assume), so it's used for matching file names. preg_match() matches a pattern (if one exists) in a string. Link to comment https://forums.phpfreaks.com/topic/123134-preg_match-ahhhhhhhhhhhhh/#findComment-635900 Share on other sites More sharing options...
effigy Posted September 8, 2008 Share Posted September 8, 2008 preg uses PCRE patterns; fnmatch uses shell wildcard patterns. P.S. Always quote variables that are used in expressions: $regex = '/' . preg_quote($search) . '/i'; Link to comment https://forums.phpfreaks.com/topic/123134-preg_match-ahhhhhhhhhhhhh/#findComment-636574 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.