title: PHP return preg_replace() inside of foreach?
body: <p>hey,
  i have no idea what I'm doing wrong.
  I have a regexp pattern that looks for a youtube url inside or [track=url].</p>
  
  <p>I'm returning a youtube embed-code if the regexp is matched. I need to have a unique ID for each video. I'm creating this ID with a simple count variable inside my preg_match_all foreach loop.</p>
  
  <p>The $uniqueID that I need for each video works just fine. If I have 3 [track=url] inside my $content I get 3 different id's echoed out (player_1, player_2, player_3, etc...)</p>
  
  <p>However ONLY MAJOR PROBLEM that I have is that I have no idea how I'm using the preg_replace in that case. I need to return the embedCode for each video with each the unique ID that I'm creating.</p>
  
  <pre><code>&lt;?php
  
      $youtubeUrl = "/(\[TRACK=)((http|https)(\:\/\/)(www|it|co\.uk|ie|br|pl|jp|fr|es|nl|de)(\.youtube\.)(com|it|co\.uk|ie|br|pl|jp|fr|es|nl|de)([a-zA-Z0-9\-\.\/\?_=&amp;;]*))(\])/si";
  
      $search = preg_match_all($youtubeUrl, $content, $matches, PREG_OFFSET_CAPTURE);
      $i = 0;
  
      foreach ($matches[8] as $match) {
  
          $watch = $match[0];
  
          //unique id
          $uniqueID = "player_" . $i; // player_0, player_1, player_2 ...
  
          //final video url
          $video = $uri . $watch;
  
          echo $video . "&lt;br/&gt;"; //correct 3 times different
  
          $content = preg_replace($youtubeUrl, embedCode($video, $uniqueID), $content);
          // three times player_0
  
          $i++;
      }
  
      //$content = preg_replace($youtubeUrl, embedCode($video, $uniqueID), $content);
      // three times player_3
  
      return $content;
  
  ?&gt;
  </code></pre>
  
  <p>Any idea what I need to do here? I'm helpless!
  If I call the preg_replace inside the loop I get three times the embed_code for player_0, if I call it outside the foreach loop I get three time player_3.</p>
  
  <p>What am I doing wrong here! Thank you very much for your help.</p>