RopeADope Posted March 9, 2011 Share Posted March 9, 2011 Hi all. I have the following function to display directories but I need help to reverse my logic and develop a proper expression. if(preg_match("/[.]/i",$value)){ echo ""; }else{ echo $value . " "; }; What I need it to do is check if $value is a directory and echo the directory name, else echo nothing. Right now I have it backwards (not supposed to check for a negative condition right?). What would the regex expression look like to show all directories AND exclude certain directories? I tried... preg_match("/[.][code=php:0][css]/i",$value) But this printed out nothing. The directories "php" and "css" are the ones I don't want echo'ed to the screen. Quote Link to comment https://forums.phpfreaks.com/topic/230134-preg_match-to-show-directories-and-exclude-certain-directories/ Share on other sites More sharing options...
HuggieBear Posted March 9, 2011 Share Posted March 9, 2011 Can you not just use is_dir() Quote Link to comment https://forums.phpfreaks.com/topic/230134-preg_match-to-show-directories-and-exclude-certain-directories/#findComment-1185233 Share on other sites More sharing options...
RopeADope Posted March 9, 2011 Author Share Posted March 9, 2011 I tried... if(is_dir($value)){ if($value=="zDash"){ echo ""; }else{ echo $value . " "; }; }; Which works...sort of. I still need to figure out how to add more parameters to match to the $value. I tried $value=="php" or "css" in the if() statement but no luck. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/230134-preg_match-to-show-directories-and-exclude-certain-directories/#findComment-1185237 Share on other sites More sharing options...
sasa Posted March 10, 2011 Share Posted March 10, 2011 try $value=="php" or $value=="css" Quote Link to comment https://forums.phpfreaks.com/topic/230134-preg_match-to-show-directories-and-exclude-certain-directories/#findComment-1185388 Share on other sites More sharing options...
RopeADope Posted March 10, 2011 Author Share Posted March 10, 2011 That worked. Just out of curiosity, can I use an array as a comparison argument? e.g. $matches=array("php","css"); if($value==$matches){ Do stuff... } If not, is there a way to compare $value to several values in an array? Long term, I'm thinking about putting the names of the directories to show into a MySQL table and match against those names. The software that this will be in will have different folders to show based on the client so I'll need a way to delineate which folders to show. Quote Link to comment https://forums.phpfreaks.com/topic/230134-preg_match-to-show-directories-and-exclude-certain-directories/#findComment-1185789 Share on other sites More sharing options...
sasa Posted March 11, 2011 Share Posted March 11, 2011 for compare with array use in_array() function Quote Link to comment https://forums.phpfreaks.com/topic/230134-preg_match-to-show-directories-and-exclude-certain-directories/#findComment-1186006 Share on other sites More sharing options...
RopeADope Posted March 11, 2011 Author Share Posted March 11, 2011 Here's the full bit of code: $file=scandir($_SERVER['DOCUMENT_ROOT']); $matches=("zDash","php","xampp","wedding",".","..","css","input_forms"); foreach($file as $value){ if(is_dir($value)){ if(in_array($value,$matches){ echo ""; }else{ $value=preg_replace("/_/"," ",$value); $value=ucwords($value); echo "<a href=\"/$value/\">" . $value . "</a>"; }; }; }; Any ideas why its not working? Quote Link to comment https://forums.phpfreaks.com/topic/230134-preg_match-to-show-directories-and-exclude-certain-directories/#findComment-1186281 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.