title: Regex to identify phone number
body: <p>I have a requirement where I have to hide phone number in messages provided by users. I already have one regular expression which is as follows:</p>
  
  <pre><code>/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})/
  </code></pre>
  
  <p>But this can only identify mobile numbers of following formats:</p>
  
  <pre><code>9876543210
  </code></pre>
  
  <p>I want it to cover following formats too:</p>
  
  <blockquote>
    <p>987 654 3210</p>
    
    <p>9 8 7 6 5 4 3 2 1 0</p>
    
    <p>(987) 654 3210</p>
    
    <p>(987) (654) (3210)</p>
  </blockquote>
  
  <p>In all the above formats, spaces can be replaced by either '-' or '.'. Also, '(' and ')' can be replaced by '[' and ']'.</p>
  
  <p>Also, is it possible to identify phone numbers which are mentioned with strings instead of digits, like</p>
  
  <blockquote>
    <p>Nine eight seven six five four three two one zero</p>
    
    <p>Any combination of digits and strings</p>
  </blockquote>
  
  <p><strong>EDIT:</strong> Adding my function which is hiding contact numbers if any from content:</p>
  
  <pre><code>function hide_contact_number($description) {
  // Find contact number and hide it!
  $regex = "/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})/";
  /*$regex = "/[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{4})[\)\]]?|([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])/";*/
  if(preg_match_all($regex, $description, $matches, PREG_OFFSET_CAPTURE)) {
      foreach($matches as $matchkey =&gt; $match) {
          foreach($match as $key =&gt; $value) {
              $index = 0;
              $length = 0;
              if(is_array($value)) {
                  if(is_numeric($value[0]) &amp;&amp; strlen($value[0]) &gt;= 10) {
                      $index = $value[1];
                      $length = strlen($value[0]);
                  } else if(strlen($value[1]) &gt;= 10) {
                      $index = $value[0];
                      $length = strlen($value[1]);
                  } else {
                      // TODO: Do nothing
                  }
              }
  
              if($length &gt; 0) {
                  // length - 2 =&gt; 2 places before end of email id including 1 of index + 1
                  $description = substr_replace($description, str_repeat("*", $length-2), $index+1, $length-2);
              }
          }
      }
  }
  
  return $description;
  </code></pre>
  
  <p>}</p>
  
  <p>The above function does not identify and hide all the number sequences I have mentioned. Even @CCH's solution does not help. Is anything wrong with this function?</p>