Closed Thread Icon

Topic awaiting preservation: [php] How do you use a variable in a preg_match(/$var/) ? (img gallery) (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=23961" title="Pages that link to Topic awaiting preservation: [php] How do you use a variable in a preg_match(/$var/) ? (img gallery) (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: [php] How do you use a variable in a preg_match(/$var/) ? (img gallery) <span class="small">(Page 1 of 1)</span>\

 
ninmonkeys
Nervous Wreck (II) Inmate

From:
Insane since: May 2004

posted posted 11-08-2004 22:49

I think the problem is This line

code:
if(preg_match("/^$img_path_extless\.(png|jpg|gif)$/i", "$img_path", $matches))

I believe it's not using the content of the variable. I thought "\$var" or "\${var}" was supposed to work but it doesn't seem to. Or, maybe I have a logic error.

I'm trying to write a script that will generate an image gallery. My directory structure looks like this

code:
images/
oil/
thumbs/
image.gif
image2.gif
image.gif
image2.jpg
pencil/
thumbs/
etc.../
thumbs/

The problem is the thumbnails are always gifs "images/oil/image2.gif" but the full size image is not always a gif. ex: "images/oil/image2.jpg"

Here's my code, sorry it's kind of messy, I've been stuck on this "simple" script for too long

code:
<?php

/* image gallery script */
function printThumbs($img_header="Header", $img_dir="images")
{
//print header
echo "\n<h2>$img_header</h2>";
//start thumbs
echo "\n<div class=\"thumbs\">";

//read "$img_dir/thumbs", foreach img, print thumb
if($handle = opendir("$img_dir/thumbs"))
{
while(false !== ($file = readdir($handle)))
{
//for every entry, check if it's an image
if(preg_match("/(png|jpg|gif)$/i","$file", $matches))
{
$thumb_path = "$img_dir/thumbs/$file";
$img_path = "$img_dir/$file";
//get extensionless img name
preg_match("/(.*)\.(png|jpg|gif)$/i", "$img_path", $matches);
$img_path_extless = $matches[1]; //$img_path with no extension

//check if $img_path exists (if !exist, img has different extension)
if(! file_exists($img_path))
{
$img_path_url = "art.php"; //just incase there is no match

//read each file in dir $img_dir into $img_list
if($imgHandle = opendir($img_dir))
{
while(false !== ($imgFile = readdir($imgHandle)))
{
//check for match
if(preg_match("/^$img_path_extless\.(png|jpg|gif)$/i", "$img_path", $matches))
{
echo "match: $matches[1]";
$img_path_url = $matches;

}
echo "$img_path_extless: ";
echo "$imgFile<br />";
}
closedir($imgHandle);
}
else
{
echo "<p>Error reading $img_dir</p>";
}
}
else
{
$img_path_url = $img_path;
}
//print img link
echo "\n<a href=\"$img_path_url\">";
//print thumbnail
echo "\n<img src=\"$thumb_path\" alt=\"$file\" title=\"$file\" width=\"75\" height=\"75\"/>";
echo "</a>";
}
}
//I read the dir, so close it
closedir($handle);
}
else
{
echo "<p>Error reading dir: $img_dir/thumbs</p>";
}

//end thumbs
echo "\n</div>";
}

printThumbs("Pen", "images/pen");
printThumbs("Pencil", "images/pencil");
printThumbs("Photographs", "images/photograph");
printThumbs("Oil Paintings", "images/oil");
printThumbs("Water Color", "images/watercolor");
?>

Line 69 is the preg_match I have at the beginning of this post, the $img_path_extless one. The errors I get

code:
Warning: Unknown modifier 'p' in /home/jake/public_html/school/art.php on line 69 (many times)
Warning: Unknown modifier 'o' in /home/jake/public_html/school/art.php on line 69 (many times)
Warning: Unknown modifier 'w' in /home/jake/public_html/school/art.php on line 69 (many times)

Thanks for any help,
--monkey

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 11-09-2004 01:32

Concatonation should work, so instead of the

code:
if(preg_match("/^$img_path_extless\.(png|jpg|gif)$/i", "$img_path", $matches))



that you have, try

code:
if(preg_match("/^".$img_path_extless."\.(png|jpg|gif)$/i", $img_path, $matches))

ninmonkeys
Nervous Wreck (II) Inmate

From: mn
Insane since: May 2004

posted posted 11-09-2004 04:44

I still get the same error: Warning: Unknown modifier 'p' in /home/jake/public_html/school/art.php on line 69

Does the error mean it's not reading the variable? (Not reading the content)

Any other ideas?

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 11-09-2004 09:31

that's because there's a '/' in your $img_path_extless. Which you use as a regexps delimiter - wich of course will not work.
ever tried a print "/^".$img_path_extless."\.(png|jpg|gif)$/i" ? That would've shown you your error.
Use something else, '!' or so instead of the '/' to delimit your regexps.

ninmonkeys
Nervous Wreck (II) Inmate

From: mn
Insane since: May 2004

posted posted 11-10-2004 00:06

Tyberius Prime said:That fixes my regex, where did you find that information? (That ! can be used inplace of / in a regex)

I fixed my logic error too, so now the script is working perfectly

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 11-10-2004 00:11

well the php->prce (perl style regexps overview) page states:

quote:
The expression should be enclosed in the delimiters, a forward slash (/), for example. Any character can be used for delimiter as long as it's not alphanumeric or backslash (\).



which is probably where I knew that from.

« BackwardsOnwards »

Show Forum Drop Down Menu