Question

I'm trying to setup a little gallery based on david walsh's howto.

For now, this gallery provides alt tag using images filenames. I need a way to use a text file, associated with my images, something like this :

myprettypicture.jpg = "Here's a pretty picture"  
mynotsoprettypicture.jpg = "Here's a not so pretty picture"  

Then retrieve this to put as ALT tags with the php gallery. So far, I've looked into several galleries and there's a few using this method, but i have no idea how to use it with my actual gallery...
Do you know a script or a simple method close to what i'm searching for ?

Thank you !

Was it helpful?

Solution

If I understand You correctly, You've got a source_file.htm like the one below.

<!DOCTYPE html>
<html>
<body>
<img src="myprettypicture.jpg" alt="myprettypicture.jpg" height="50" width="50">
<img src="mynotsoprettypicture.jpg" alt="mynotsoprettypicture.jpg" height="50" width="50">
</body>
</html>

Now, it is better to store the replacements for the alt tags of your images in an images.ini file like that.

myprettypicture.jpg = "Here's a pretty picture"
mynotsoprettypicture.jpg = "Here's a not so pretty picture"

Now You can run this PHP script:

<?php
$ini_array = parse_ini_file("images.ini"); // load "images.ini" into array
foreach($ini_array as $key => $val)
{
  $img[] = 'alt="' . $key . '"';
  $alt[] = 'alt="' . $val . '"';
}
$source = file_get_contents('source_file.htm'); // get Your "source_file.htm"
$target = str_replace($img, $alt, $source);     // make all required replacements
file_put_contents('target_file.htm', $target);  // save the result to "target_file.htm"
?>

to get the target_file.htm

<!DOCTYPE html>
<html>
<body>
<img src="myprettypicture.jpg" alt="Here's a pretty picture" height="50" width="50">
<img src="mynotsoprettypicture.jpg" alt="Here's a not so pretty picture" height="50" width="50">
</body>
</html>

Reference: str_replace, parse_ini_file, file_get_contents, file_put_contents

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top