preg_match_all and preg_replace foreach loop
<p>I'm trying to convert [quote="author"]text[/quote] to a formatted div block.</p>

<p><code>$p_text</code>:</p>

<pre><code>[quote="person1"]Hello![/quote]
[quote="person2"]Hi![/quote]
</code></pre>

<p>Function:</p>

<pre><code>function bbCode($p_text){

      $pattern = '/\[quote="(.+?)"\]/'; // matches the author name, i.e. person1
      preg_match_all($pattern, $p_text, $matches);

      $authorCounter = 0;

      foreach ($matches as $matchgroup) {

        $author = $matchgroup[$authorCounter];

        $pattern1 = '/\[quote=".+?"\]/'; // captures [quote="..."]
        $replacement1 = '&lt;div class="quote"&gt;&lt;strong class="quote-author"&gt;' . $author . ' wrote:&lt;/strong&gt;&lt;br&gt;';
        $p_text = preg_replace($pattern1, $replacement1, $p_text);

        $authorCounter++;
      }

      $pattern2 = '/\[\/quote\]/'; // captures [/quote]
      $replacement2 = '&lt;/div&gt;';
      $p_text = preg_replace($pattern2, $replacement2, $p_text);

      return $p_text;

}
</code></pre>

<p>This is replacing both quote's authors with "person2" because the second <code>foreach</code> iteration replaces the text again(?). How could I make sure each quote has the correct person's name?</p>