fizix Posted February 1, 2007 Share Posted February 1, 2007 Ok, I've started to write some code to make all images in a string thumbnails, then wrap link tags around them to link to the full-size picture. This is what I've got so far: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=windows-1250"> <title>IMG Test</title> <link rel="stylesheet" href="./styles/default/thumbnailviewer.css" type="text/css" /> <script src="./styles/default/thumbnailviewer.js" type="text/javascript"></script> </head> <body> <?php //Two test images $content = "<img vspace=\"0\" hspace=\"0\" border=\"0\" align=\"bottom\" src=\"localhost/images/test.jpg\" />\n<img vspace=\"0\" hspace=\"0\" border=\"0\" align=\"bottom\" src=\"localhost/images/fizix_crop.ba.jpg\" />"; $src = "src=\""; $thumbnail = "./phpthumb/phpThumb.php?h=75&src="; //pattern to get links to images from image tags $pattern = '!<img.*?src="(.*?)".*?/?>!'; $img = "<img"; $frontlink = "<a href=\""; $backlink = "\" rel=\"thumbnail\">"; if (strpos($content, $img) !== false) { //put all image links in $urls preg_match_all($pattern, $content, $urls); //append thumbnail function to image link $content = str_replace($src,$src.$thumbnail,$content); //place link to full size image before thumbnail But this is where my question arrises. I want to do something like this on the next line: $content = str_replace($img,$frontlink.$urls[1][$x].$backlink.$img,$content); But I don't know how to increment $x. I need to increment $x every time str_replace replaces a string. Here's the rest of the code: } echo $content; ?> </body> </html>code] Quote Link to comment https://forums.phpfreaks.com/topic/36632-incrementing-with-str_replace/ Share on other sites More sharing options...
fizix Posted February 1, 2007 Author Share Posted February 1, 2007 Also, if you can think of a better way to do anthing else I'm doing in this snippet, I won't be offended if you wind up completely re-writing it! Quote Link to comment https://forums.phpfreaks.com/topic/36632-incrementing-with-str_replace/#findComment-174565 Share on other sites More sharing options...
HuggieBear Posted February 1, 2007 Share Posted February 1, 2007 My advice here would be to use preg_replace_callback() I've just written some code that you might find useful. I'll forward it onto you at the weekend. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/36632-incrementing-with-str_replace/#findComment-174705 Share on other sites More sharing options...
fizix Posted February 1, 2007 Author Share Posted February 1, 2007 OK, I figured out a way to do it but it's pretty hacked: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=windows-1250"> <title>IMG Test</title> <link rel="stylesheet" href="./styles/default/thumbnailviewer.css" type="text/css" /> <script src="./styles/default/thumbnailviewer.js" type="text/javascript"></script> </head> <body> <?php //Two test images $content = "Image 1: <img vspace=\"0\" hspace=\"0\" border=\"0\" align=\"bottom\" src=\"/images/test.jpg\" />\nImage 2: <img vspace=\"0\" hspace=\"0\" border=\"0\" align=\"bottom\" src=\"/images/fizix_crop.ba.jpg\" />"; $src = "src=\""; $thumbnail = "./phpthumb/phpThumb.php?h=75&src="; //pattern to get links to images from image tags $pattern = '!<img.*?src="(.*?)".*?/?>!'; $img = "<img"; $frontlink = "<a href=\""; $backlink = "\" rel=\"thumbnail\" />"; $closelink = "</a />"; if (strpos($content, $img) !== false) { //put all image links in $urls preg_match_all($pattern, $content, $urls); //make array with placeholders for ($x=0;$x<count($urls[0]);$x++) { $placehold[$x]="%$x"; } //append thumbnail function to image link $newcontent = str_replace($src,$src.$thumbnail,$content); //put all thumbnail links in $thumburls preg_match_all($pattern, $newcontent, $thumburls); //remove all image tags from $content and replace with placeholders $content = str_replace($urls[0],$placehold,$content); for ($x=0;$x<count($urls[0]);$x++) { $content=str_replace($placehold[$x],$frontlink.$urls[1][$x].$backlink.$thumburls[0][$x].$closelink,$content); } } echo $content; ?> </body> </html> Any suggestions for improving this are welcome! Quote Link to comment https://forums.phpfreaks.com/topic/36632-incrementing-with-str_replace/#findComment-174718 Share on other sites More sharing options...
HuggieBear Posted February 2, 2007 Share Posted February 2, 2007 Yeah, that looks pretty nasty. I'll post it for you at the weekend. Huggie Quote Link to comment https://forums.phpfreaks.com/topic/36632-incrementing-with-str_replace/#findComment-175109 Share on other sites More sharing options...
fizix Posted February 18, 2007 Author Share Posted February 18, 2007 Yeah, that looks pretty nasty. I'll post it for you at the weekend. Huggie It's been a few weeks, do you still plan on posting this code? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/36632-incrementing-with-str_replace/#findComment-188003 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.