XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).
Moderator: General Moderators
rhosk
Forum Commoner
Posts: 30 Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA
Post
by rhosk » Thu Nov 11, 2004 8:07 am
With the code below, I can manipulate to either list files that only has "sample" in the file name or list everything but "sample".
How can I apply 2 filters? I want to list everything except "sample" and "perm" in the filename. Thanks.
Code: Select all
<?
$xml = '<?xml version="1.0" encoding="UTF-8" ?>';
$xml .= '<slideshow><settings><image_folder></image_folder><time>3</time><fade>1</fade><repeat>true</repeat><captions>false</captions></settings><images>';
create_tree("ssimages", '/^sample/');
function create_tree($file_dir, $filter = '') {
global $xml;
if ($handle = @opendir($file_dir))
{
$list = array();
while (false !== ($file = @readdir($handle))) {
if ($file != "." && $file != "..") {
if( !empty($filter) && !is_dir($file_dir . '/' . $file) )
//if( !preg_match($filter, $file) ) // get only "sample"
if( preg_match($filter, $file) ) //everything but sample
continue;
$list[] = $file;
}
}
foreach($list as $file) {
if( is_dir($file_dir . '/' . $file) ) {
create_tree($file_dir . "/" . $file, $filter);
}
else
$xml .= '<image>' . '<file>' . $file_dir . '/' . $file . '</file>' . '<caption>' . '</caption>' . '</image>' . "\n";
}
closedir($handle);
}
}
$xml .= '</images></slideshow>';
echo $xml;
?>
timvw
DevNet Master
Posts: 4897 Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium
Post
by timvw » Thu Nov 11, 2004 10:17 am
Code: Select all
preg_match("/$filter|$filter2/", $file)
rhosk
Forum Commoner
Posts: 30 Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA
Post
by rhosk » Thu Nov 11, 2004 11:52 am
timvw wrote: Code: Select all
preg_match("/$filter|$filter2/", $file)
Well, that didn't work. Please let me know if I'm close.
Code: Select all
<?
$xml = '<?xml version="1.0" encoding="UTF-8" ?>';
$xml .= '<slideshow><settings><image_folder></image_folder><time>3</time><fade>1</fade><repeat>true</repeat><captions>false</captions></settings><images>';
create_tree("ssimages", '/^sample/', '/^perm/');
function create_tree($file_dir, $filter = '', $filter2 = '') {
global $xml;
if ($handle = @opendir($file_dir))
{
$list = array();
while (false !== ($file = @readdir($handle))) {
if ($file != "." && $file != "..") {
if( !empty($filter) && !is_dir($file_dir . '/' . $file) )
if( preg_match("/$filter|$filter2/", $file) ) //everything but sample and perm
continue;
$list[] = $file;
}
}
foreach($list as $file) {
if( is_dir($file_dir . '/' . $file) ) {
create_tree($file_dir . "/" . $file, $filter, $filter2);
}
else
$xml .= '<image>' . '<file>' . $file_dir . '/' . $file . '</file>' . '<caption>' . '</caption>' . '</image>' . "\n";
}
closedir($handle);
}
}
$xml .= '</images></slideshow>';
echo $xml;
?>
This is still giving me every file in the folder
timvw
DevNet Master
Posts: 4897 Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium
Post
by timvw » Thu Nov 11, 2004 4:41 pm
rhosk wrote:
Well, that didn't work. Please let me know if I'm close.
It works
But your code has other issues :p
Code: Select all
$filter1 = "hihi";
$filter2 = "bar";
$words = array();
$words[] = " i was walking";
$words[] = "he is hihi funny";
$words[] = "we had drinks at the bar";
foreach($words as $word)
{
if (preg_match("/$filter1|$filter2/", $word))
{
echo "match: $word <br>";
}
else
{
echo "no match: $word <br>";
}
}
rhosk
Forum Commoner
Posts: 30 Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA
Post
by rhosk » Thu Nov 11, 2004 7:02 pm
This doesn't help me. Sorry, but I'm lost. I still can't get it to work.
timvw
DevNet Master
Posts: 4897 Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium
Post
by timvw » Fri Nov 12, 2004 7:05 am
change your code to: (so get rid of the / / )
Code: Select all
create_tree("ssimages", '^sample', '^perm');
rhosk
Forum Commoner
Posts: 30 Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA
Post
by rhosk » Fri Nov 12, 2004 7:28 am
OMG, I can't believe it was that simple - geez. I see what you were trying to do for me and I appreiciate it. I do learn by trial and error, but mostly by getting it right and seeing WHY. Still learning this stuff.
Thanks for the tutor, Tim! And good luck with your 150 goal!!