title: PHP validate German Tax Id (Steueridentifikationsnummer)
body: <p>The German Tax Id (<a href="http://de.wikipedia.org/wiki/Steueridentifikationsnummer" rel="nofollow noreferrer">Steueridentifikationsnummer</a>) has the following properties:</p>
  
  <ol>
  <li>It has 11 digits</li>
  <li>First digit cannot be 0</li>
  <li>In the first ten digits: one number occurs exactly twice or thrice,  one or two digits appear zero times and the other digits appear exactly once</li>
  <li>The last digit is a checksum <a href="http://de.wikipedia.org/wiki/Steueridentifikationsnummer#Aufbau_der_Identifikationsnummer" rel="nofollow noreferrer">Example Code for Checksum</a></li>
  </ol>
  
  <p>The third bulletpoint is a little difficult for me to solve in an elegant way. I already have the code for the other three bulletpoints, but would love to get input for the last one, so that this could be a small little reference for other people.</p>
  
  <pre><code># validate tax number
          $taxNumber = $_POST['taxNumber'];
          echo preg_replace("/[^0-9]/", "", $taxNumber);
  
          if (strlen($taxNumber != 11)) {
              # 11 digits
              $taxNumberValid = false;
          } else if ($taxNumber[0] == "0") {
              # first digit != 0
              $taxNumberValid = false; 
          } else {
              # one digit two times, one digit zero times
  
  
              # checksum
              $numbers = str_split($taxNumber);
              $sum = 0;
              $product = 10;
              for ($i = 0; $i &lt;= 9; $i++) {
                  $sum = ($numbers[$i] + $product) % 10;
                   if ($sum == 0) {
                       $sum = 10;
                   }
                   $product = ($sum * 2) % 11;
              }
              $checksum = 11 - $product;
              if ($checksum == 10) {
                  $checksum = 0;
              }
  
              if ($taxNumber[10] != $checksum) {
                  $taxNumberValid = false;
              }
          }
  </code></pre>