title: Formatting the data matched by a regular expression before updating a database
body: <p>I have a validation that accepts phone numbers in a specific manner and dumps them in a database but it does not convert them the way I want.</p>
  
  <p>For example, if I enter <code>9999999999</code> or <code>09999999999</code> or <code>+919999999999</code> as a phone number, it gets into the database the way I have entered it.</p>
  
  <p>How can I format it instead in the <code>+919999999999</code> style irrespective of how the user has entered it?</p>
  
  <pre><code>function validate_phone($input){
      $input = trim($input); //get rid of spaces at either end
      if (preg_match('/^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}$/',$input) == 1){
          return $input;
      }else{
          return false;
      }
  }
  </code></pre>