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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.