title: How to change string every nth occurance in php
body: <p>JetPack by Automatic is a free image editing SaaS tool for WordPress users. One of its many features is that you can alter the subdomain of the image url being returned to enhance parallel loading of multiple srcs. The three domain options are: "i0", "i1" and "i2". </p>
  
  <p>If each photo appears in a foreach statement how can I rotate between the three subdomains so that img1 &amp; img4 use "io", img2 &amp; img5 use "i1" and img3 uses "i2"?</p>
  
  <pre><code>foreach ($images as $attachment) {
  
      // If using Photon select full-size photos
      if ( $use_photon == 'one' )
          print $before_img . '&lt;img src="http://i1.wp.com/'. str_replace('http://', '', $$fullsizeurl ) . $resize_img_to .'"&gt;' . $after_img;
  
      // Else select 'medium' thumbnail
      else
          print $before_img . wp_get_attachment_image( $attachment-&gt;ID, 'medium' ) . $after_img;
  
  } // foreach
  </code></pre>