title: How to make math inside preg_replace php
body: <p>I have the following string</p>
  
  <pre><code>$str = ".block_1 {width:100px;}.block_2 {width:200px;}.block_3 {width:300px;}";
  </code></pre>
  
  <p>I want to replace the px values with percentage values according to this formula (pixelvalue / 960) *100</p>
  
  <p>I know that with such regular expression ([0-9]+px) I can find all values + px but then I need to run through it again replacing it with  (pixelvalue / 960) *100.'%'</p>
  
  <p>Hope you understand what I mean and thank you for any help.</p>
  
  <p>Ok, here is the solution:</p>
  
  <pre><code>$str = preg_replace_callback(
          '([0-9]+px)',
          function ($matches) {            
              return ((str_replace('px','',$matches[0])/960)*100).'%';
          },
          $str
      );
      echo $str;
  </code></pre>