Page 1 of 1

eregi or preg_match with $QUERY_STRING

Posted: Sun Feb 09, 2003 12:38 am
by lvzrdoz
:?:

I wrote a pretty useful (quick and dirty) clickable file listing originally just for GIF and JPGs. I wanted to change the program to specify filetypes on the address line like:

--> http://www.xyz.com/program.php?gif|jpg|png|jpeg

Problem is I can't figure out how to correctly use eregi or preg_match with $QUERY_STRING. I've tried various ESCAPING methods but just get the "Parse error:" I'm no expert on regex or ereg or preg_match. Anybody got any ideas?

Bob from Las Vegas

---------------------------- begin code ----------------------------------

<HTML><HEAD><STYLE>
BODY,A { font-family: sans-serif; font-size: 9px; overflow: hidden;}
</STYLE><BODY topmargin=1 leftmargin=1 bgcolor=#D0FFF0
link=#4A49A8 alink=#4A49A8 vlink=#4A49A8>

<?php
$curdir = getcwd(); $inputarray = opendir($curdir); $resultarray = array();

while($var = readdir($inputarray))
{
if(!is_dir($curdir . "/" . $var))
{
eregi("($QUERY_STRING)",$var) $resultarray[]=$var; // tried preg_match too!
// if(stristr($var,".gif")) $resultarray[]=$var; // this way works
}}

function isnorm($var) { return (strlen($var) < 21); }
sort( $norm = array_filter($resultarray,"isnorm") );
function iswide($var) { return (strlen($var) > 20); }
sort( $wyde = array_filter($resultarray,"iswide") );

function do_dir($kol, $which, $alen) {
echo "<TABLE BORDER=1 BORDERCOLOR=BLACK CELLSPACING=0 CELLPADDING=0><TR><TD>
<TABLE VALIGN=TOP bgcolor=#DEDFF6>\n" ;
for($x = 0; $x < $alen; $x+=$kol)
{
echo "\n<TR>";
for($k = 0; $k < $kol; $k++)
{
echo "<TD><A HREF=# onclick=window.open('" . $which[$x+$k] . "','imgs','width=600,height=300');> " . $which[$x+$k] . " </A></TD>\n";
}
echo "</TR>";
} echo "\n</TABLE></TD></TR></TABLE><BR CLEAR=ALL>\n";
}
echo " IMAGES in " .$curdir . "/";
do_dir(9,$norm,sizeof($norm));
do_dir(5,$wyde,sizeof($wyde));
?>
</BODY>
</HTML>

Posted: Sun Feb 09, 2003 3:02 am
by lazy_yogi
if preg_match('/^[a-zA-Z0-9-_]+\.(gif|jpg|png)$/', $filename)
// do stuff

this means :
starting with one or more chars that are in the group a-zA-Z0-9 or - or _
then having .
then having 'gif or png or jpg
and nothing afyer that

Alternate solution

Posted: Sun Feb 09, 2003 3:17 am
by lvzrdoz
lazy_yogi: what you have is fine but it is a fixed list, not what I needed. I still would like to see a solution using eregi or preg_match but I came up with a more familiar 'C' kind of solution:

(1) First, "explode" $QUERY_STRING into an array for comparisons:

------------------ code -------------------------

<?php if(empty($QUERY_STRING))
{ echo "<H3>&nbsp;URL .:. http://" . $SERVER_NAME . $PHP_SELF .
"?gif:jpg:jpeg:png</H3>"; exit();
}
$valid = FALSE;
$ftlen = sizeof($ftype = explode(":",$QUERY_STRING)) ;
if(empty($ftype[$ftlen - 1])) $ftlen--;

--------------------------------------------------

(2) Then perform a looping comparison:

--------------- code ----------------------------

$curdir = getcwd(); $inputarray = opendir($curdir); $resultarray = array();

while($var = readdir($inputarray))
{
if(!is_dir($curdir . "/" . $var))
{
for($x=0;$x<$ftlen;$x++)
if(stristr($var,".$ftype[$x]")) { $resultarray[]=$var; $x=99; $valid=TRUE; }

}}

---------------------------------------------------

My working solution is at http://www.vegasxyz.com/clikfilz.doc

(This is actually just straight HTML/PHP code but the .doc extension lets it load into MS Word or Wordpad instead of being interpretted by the browser.)

Bob