Google it! / Yahoo it! / Bing it! / Ask it!

popular-posts

It took me a while to try and rephrase the title, but to no avail.

Using the image.php script from my last article, I will read database entries and replace all tags with clickable thumbnails. All you need to do is have a database entry with an tag.

Read the database (I’m sure you know how to do this):

<?php
$sql = "SELECT * FROM articole WHERE post_id = '$id'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
?>

Extract the image and preg_replace it:

<?php
$content = $row['content'];

$content = preg_replace("/<img[^']*?src=\"([^']*?)\"[^']*?>/", '<a href="$1"><img src="image.php/$1?width=150&amp;height=150&amp;quality=100&amp;image=http://www.domain.com/images/$1" alt="" /></a>', $content);

echo $content;?>

The script wraps any image found in the content with an anchor and the image.php caller script. I guess you recognize the parameters from the previous article. What does $1 do? It replaces the first parameter of preg_replace with itself. Pretty easy, isn’t it?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>