preg_match and ||

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
mlecho
Forum Commoner
Posts: 53
Joined: Wed Feb 02, 2005 9:59 am

preg_match and ||

Post by mlecho »

i'm so imbarassed, but if i don't ask, i may go nuts....a while back you all helped introduce me to preg_match so i could pass over all those files that started with "." or "._"....i actually need to evolve that script to avoid the same files, and also other files (those with ".php", and ".txt)....i am trying to incorporate the || operator, but i am not having luck...am i going about this all wrong?

Code: Select all

<?php

$menuType=$_POST['menuType'];
$path=$_SERVER['SCRIPT_NAME'];
$directory="../project";
$tour=$_POST['tour'];
$openDir=opendir($directory);
$subpatterns=array("getDirs.php");
while(false!=($file=readdir($openDir))){
	if(!preg_match('/^\./',$file))|| (!preg_match('/.php/',$file)){
		$files.= "<pict>$file</pict>";
		}
		
}
closedir($openDir);

echo ("&xml=<images>");
echo $files; 
echo ("</images>&");
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Remove the closing and opening parentheses before and after the logical or operator, respectively. I believe your logical-or should be a logical-and. However your check could be performed in a single regex.

Code: Select all

/(?:^\.|\.(?:php|txt)$)/i
may work, however I haven't tested it.
mlecho
Forum Commoner
Posts: 53
Joined: Wed Feb 02, 2005 9:59 am

Post by mlecho »

ohoh can of worms....where should i start to learn about the meanings of the "?" and "$" and "i"- IE- What terminology should i use in the PHP manual to find out more about how those work in your code?

but many thanks...i will give this a try...it looks promiising
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Read the stickies in this board. If you need further examples, read the threads relating to regex linked from Useful Posts (sticky in PHP - Code), if after reading all those you're still confused, I'll try to explain any remaining questions you may have.
Post Reply