Jump to content

preg_match to show directories and exclude certain directories


RopeADope

Recommended Posts

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.

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?

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.

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.