title: Replace some string in div?
body: <p>I have string like this:</p>
  
  <pre><code>$str = '&lt;div&gt; text echo &lt;div class="code"&gt;echo "test!";&lt;/div&gt; &lt;/div&gt;';
  </code></pre>
  
  <p>How to replace an <code>echo</code> with <code>font</code> tag?</p>
  
  <p>for example on out must be string looks like that:</p>
  
  <pre><code>$str = '&lt;div&gt; text echo &lt;div class="code"&gt;&lt;font color="green"&gt;echo&lt;/font&gt; "test!";&lt;/div&gt; &lt;/div&gt;';
  </code></pre>
  
  <p>I've tried</p>
  
  <pre><code>$str = preg_replace("/&lt;div class='code'&gt;(echo)&lt;\/div&gt;/Usi",'&lt;font color="green"&gt;echo&lt;/font&gt;',$str);
  </code></pre>
  
  <p>but make no sense, so how I can do it?</p>
  
  <p><strong>update</strong> so seems this is a <em>solution</em>:</p>
  
  <pre><code>$str = '&lt;div&gt; text echo &lt;div class="code"&gt;echo "test!"; echo "very test!";&lt;/div&gt; &lt;/div&gt;';
  
  /*$str = preg_replace('/(&lt;div class="code"&gt;.*?)(echo)(.*?&lt;\/div&gt;)/si',
         '$1&lt;font color="green"&gt;$2&lt;/font&gt;$3', $str);*/
  
  $str = preg_replace_callback(
      '/(&lt;div class="code")(.*?)(&lt;\/div&gt;)/si',
      function ($matches) {
          return $matches[1].str_replace('echo','&lt;font color="green"&gt;echo&lt;/font&gt;',$matches[2]).$matches[3];
      },
      $str);
  
  echo $str;
  </code></pre>