title: How to convert a Roman numeral to integer in PHP?
body: <p>Using PHP, I'd like to convert a string containing a Roman number into its integer representation. I need this because I need to make calculations on them.</p>
  
  <p><a href="http://en.wikipedia.org/wiki/Roman_numerals" rel="noreferrer">Wikipedia on Roman numerals</a></p>
  
  <p>It would suffice to only recognize the basic Roman numeral characters, like:</p>
  
  <pre><code>$roman_values=array(
      'I' =&gt; 1,
      'V' =&gt; 5,
      'X' =&gt; 10,
      'L' =&gt; 50,
      'C' =&gt; 100,
      'D' =&gt; 500,
      'M' =&gt; 1000,
  );
  </code></pre>
  
  <p>That means the highest possible number is 3999 (MMMCMXCIX). I will use <code>N</code> to represent zero, other than that only positive integers are supported.</p>
  
  <p>I cannot use the PEAR library for Roman numbers.</p>
  
  <p>I found this great question on SO on how to test whether the string contains a valid Roman numeral: </p>
  
  <p><a href="https://stackoverflow.com/questions/267399/how-do-you-match-only-valid-roman-numerals-with-a-regular-expression">How do you match only valid roman numerals with a regular expression?</a></p>
  
  <p>What would be the best way of coding this?</p>