title: PHP: Replace rows depending regex group values?
body: <p>I have this text string:</p>
  
  <pre><code>[000] Foo
  [???] Foo
  [020] Foo
  [???] Foo
  [045] Foo
  [???] Foo
  [???] Foo
  [110] Foo
  [???] Foo
  </code></pre>
  
  <p>I need to check the [XXX] values for every row, but it needs to be smart:</p>
  
  <p>If a <code>???</code> value is between <code>0**</code> rows > replace it to <code>0??</code>.</p>
  
  <p>Desired result:</p>
  
  <pre><code>[000] Foo
  [0??] Foo
  [020] Foo
  [0??] Foo
  [045] Foo
  [???] Foo
  [???] Foo
  [110] Foo
  [???] Foo
  </code></pre>
  
  <p>Would I need <code>preg_replace_callback()</code>?</p>
  
  <p>In a further step I want to expand it to logic "between 1XX and 2XX" etc, but I may be able to adapt that from a posted solution.</p>
  
  <p>Test:</p>
  
  <pre><code>&lt;?php
  
  // string
  $s = '[000] Foo
  [???] Foo
  [020] Foo
  [???] Foo
  [045] Foo
  [???] Foo
  [???] Foo
  [110] Foo
  [???] Foo';
  
  // _preg_replace magic
  
  
  // output
  echo 'After replacement:';
  echo '&lt;pre&gt;' . $s . '&lt;/pre&gt;';
  
  echo 'Desired result:';
  echo '&lt;pre&gt;[000] Foo
  [0??] Foo
  [020] Foo
  [0??] Foo
  [045] Foo
  [???] Foo
  [???] Foo
  [110] Foo
  [???] Foo&lt;/pre&gt;';
  
  ?&gt;
  </code></pre>