Hướng dẫn roman to integer php
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. Show Wikipedia on Roman numerals It would suffice to only recognize the basic Roman numeral characters, like:
That means the highest possible number is 3999 (MMMCMXCIX). I will use I cannot use the PEAR library for Roman numbers. I found this great question on SO on how to test whether the string contains a valid Roman numeral: How do you match only valid roman numerals with a regular expression? What would be the best way of coding this? asked Jun 7, 2011 at 13:05 3 How about this:
which should output 3999 for the supplied
You might want to do some validation first as well :-) answered Jun 7, 2011 at 13:45 andybandyb 42.9k12 gold badges121 silver badges149 bronze badges 7 I am not sure whether you've got ZF or not, but in case you (or any of you who's reading this) do here is my snippet:
kapa 76.2k20 gold badges156 silver badges174 bronze badges answered Jun 7, 2011 at 13:34 akondakond 15.6k4 gold badges34 silver badges55 bronze badges 1 This is the one I came up with, I added the validity check as well.
answered Jun 9, 2011 at 10:01 kapakapa 76.2k20 gold badges156 silver badges174 bronze badges Quick idea - go through the Roman number from right to left, if value of
Some validation of roman number should also be added, though you said that you already have found how to do it. answered Jun 7, 2011 at 13:22 binaryLVbinaryLV 8,8422 gold badges39 silver badges42 bronze badges 5 Copyrights is for this blog (btw!) http://scriptsense.blogspot.com/2010/03/php-function-number-to-roman-and-roman.html
kapa 76.2k20 gold badges156 silver badges174 bronze badges answered Jun 7, 2011 at 13:10 publikz.compublikz.com 9231 gold badge12 silver badges21 bronze badges 1 I'm late to the party, but here's mine. Assumes valid Numerals in the string, but doesn't test for a valid Roman number, whatever that is...there doesn't seem to be a consensus. This function will work for Roman numbers like VC (95), or MIM (1999), or MMMMMM (6000).
answered Feb 3, 2013 at 7:35 akTedakTed 2042 silver badges8 bronze badges Define your own schema! (optional)
Inspired by andyb's answer answered Jun 7, 2011 at 23:33 ShadShad 14.6k2 gold badges21 silver badges34 bronze badges 1 I just wrote this in about 10 mins, it's not perfect, but seems to work for the few test cases I've given it. I'm not enforcing what values are allowed to be subtracted from what, this is just a basic loop that compares the current letter value with the next one in the sequence (if it exists) and then either adds the value or adds the subtracted amount to the total:
answered Apr 2, 2012 at 16:13 1 Just stumbled across this beauty and have to post it all over:
answered Feb 19, 2013 at 10:46 3
answered May 6, 2015 at 23:10 1
answered Aug 23, 2015 at 20:02
answered May 3, 2019 at 10:51 Whew! Those are quite a few answers, and made of them are code-heavy! How about we define an algorithm for this first, before I give an answer? The Basics
The AlgorithmExample:
The DemoFull Working Demo Online The Code
answered Jan 16 at 21:54 HoldOffHungerHoldOffHunger 16.7k8 gold badges92 silver badges121 bronze badges |