Q:

str_replace count occurrences of $placeholder, replace with corresponding $variable

I've been trying to accomplish this for a few days. I have a description in each post that has placeholders manually placed where I want the corresponding image to be replaced. For example:
This is the description shortened...
[image]
[image]
Description starts again with a new paragraph continuing...

The placeholders are [image]. With every new post I upload multiple images, but each post may vary from 1-10 images, so a variable amount of [image] placeholders are placed. I have a function that gets all related images to that post and count how many images there are.
Here is the code I have so far, but what's wrong is, for the first two placeholders [image] it shows the first related image twice, then loops and shows the description again, this time with the second image replacing both [image] placeholders.
<?php
  foreach ($photos as $picture) {
    $count = count($picture);
    for($i = 1; $i<= $count; $i++) {
      $image = $picture['filename'];
      $replace = "[image]";
      $image_path = '../../content_management/image_upload/';
      $placeholders = array("$replace");
      $image_location = array('<a class="fancybox" href="' . $image_path . '' . $image . '" data-fancybox-group="gallery"><img src="' . $image_path . '' . $image . '" /></a>');
      $rawstring = $photo_article['description'];
      $new_string = $rawstring;
      $new_string = str_replace($placeholders, $image_location, $new_string, $i);
      echo $new_string;
    }
  }
?>

And what the output is:
This is the description shortened...
Image1.jpg
Image1.jpg
Description starts again with a new paragraph continuing...

This is the description shortened...
Image2.jpg
Image2.jpg
Description starts again with a new paragraph continuing...

A:

$photos = array(
    'photo1' => array(
    'filename' => 'image1.png'
    ),
    'photo2' => array(
    'filename' => 'image2.png'
    ),
);

$photo_article['description'] = "This is the description shortened...
[image]
[image]";

foreach ($photos as $picture)
{
    $image = $picture['filename'];
    $image_path = '../../content_management/image_upload/';
    $replacement = '<a class="fancybox" href="' . $image_path . '' . $image . '" data-fancybox-group="gallery"><img src="' . $image_path . '' . $image . '" /></a>';
    $photo_article['description'] = preg_replace("~\[image\]~s", $replacement, $photo_article['description'], 1);
}
    echo $photo_article['description'];

