1

I have some content in a CMS database, where all images have a click to zoom plugin.

The HTML markup for an image looks like this

<div class="large-image-outer">

<a class="fancybox-button zoomer" data-rel="fancybox-button" title="" href="http://images.website.com/prams/folder/e3-v2/review/e3-v2-introduction-1.jpg">

<div class="overlay-zoom"><img class="large-image img-polaroid" src="http://images.website.com/prams/folder/e3-v2/review/e3-v2-introduction-1.jpg" alt="" title="" />
<div class="zoom-icon"></div>
</div>

</a>

</div>

I'm trying to go through the database and replace all images on the page with thumbnails. Each thumbnail is named the same as the image, but with -thumbnail.jpg on the end. So in the example above

<img class="large-image img-polaroid" src="http://images.website.com/prams/folder/e3-v2/review/e3-v2-introduction-1.jpg" alt="" title="" />

should be replaced with

<img class="large-image img-polaroid" src="http://images.website.com/prams/folder/e3-v2/review/e3-v2-introduction-1-thumbnail.jpg" alt="" title="" />

How can I do this with PHP - I'm assuming preg replace is the answer, but it's tricky because I need to

  1. Only replace the image file name in the tag not the link to the full size image in the tag.

  2. append "-thumbnail.jpg" regardless of whether the image is a png or jpg file (all of the thumbnails are jpg, but some of the full size images are png.

Thanks

1 Answer 1

3

Some regex will help here

preg_replace('/src\=\"(.*?)\.(jpg|png)\"/', 'src="$1-thumbnail.$2"');
Sign up to request clarification or add additional context in comments.

1 Comment

I can't accept your answer for 8 minutes, but you're a legend! Thanks Carl.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.