Forum Moderators: coopster

Message Too Old, No Replies

problem matching two regular expressions using if/ elseif

         

dnb_ovst

6:47 pm on Sep 19, 2006 (gmt 0)

10+ Year Member



Hi guys.
I'm having a bit of trouble, i want to include all htm files in a directory, as well as jpegs. I've written this script with the help of users in this forum; however, the else condition does not want to work? There are indeed jpgs in the directory im looking in, but its not echoing the desired output?


<?php
$dir = 'classifieds/'."$_GET[subcategory]";
$fp = @opendir($dir);


while ($file = @readdir($fp)) {
if (preg_match("/^[a-zA-Z0-9\/\-\_]+\.htm$/", $file)) {
require_once($dir.'/'.$file);
echo "<br />";
} elseif (preg_match("/^[a-zA-Z0-9\/\-\_]+\.jpg$/", $image)) {
echo "<img src='".$dir."/".$image."'"." alt='advertisement' />";
}
}


@closedir($fp);
?>

any help would be greatly appreciated!

Psychopsia

7:23 pm on Sep 19, 2006 (gmt 0)

10+ Year Member



Hello dnb_ovst.

It doesn't work because you are using $image var instead of $file in preg_match in 'elseif' condition.

Also change the var name in 'echo' line.

dnb_ovst

7:33 pm on Sep 19, 2006 (gmt 0)

10+ Year Member



EXCELLENT! thanks alot Psychopsia.

although im unsure why it wouldnt have worked with the $image variable name...?

dnb_ovst

7:38 pm on Sep 19, 2006 (gmt 0)

10+ Year Member



disregard last post, i just noticed the while argument-
thx