Jump to content

Recommended Posts

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]

Link to comment
https://forums.phpfreaks.com/topic/36632-incrementing-with-str_replace/
Share on other sites

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!

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.