title: php replace substring with regex
body: <p>Ok, i know there are trillions of similar questions, but i'm finding it really hard to achieve this.
  I have some strings of this formats:</p>
  
  <pre><code>$x = '&lt;iframe src="[File:19]"&gt;&lt;/iframe&gt;';
  $y = '&lt;img src=[File:2212] /&gt;';
  $z = '&lt;source src="[File:42]" /&gt;';
  </code></pre>
  
  <p>I'm trying to get the id given after the <code>File:</code> and also replace the whole <code>[File:xxx]</code> with another string. I'm trying like the following, but it seems i can't fully understand the usage of preg_replace.</p>
  
  <pre><code>$file = ('&lt;iframe src="[File:134]"&gt;&lt;/frame&gt;');
  $rex = "/^.*(\[File:[0-9]{1,}\])/i" ;
  if ( preg_match($rex, $file, $match) ) {
      echo 'OK';
  }
  $file = preg_replace ($rex, "http://lala.com/la.pdf", $file);
  echo "&lt;br&gt;".htmlentities($file)."&lt;br&gt;";
  </code></pre>
  
  <p>Could you please give me some hints on how i can do this?</p>
  
  <p>Thanks in advance.</p>