3v4l.org

run code in 300+ PHP versions simultaneously
<?php function text2num($s) { // Enhanced the regex at http://www.rexegg.com/regex-trick-numbers-in-english.html#english-number-regex $reg = <<<REGEX (?x) # free-spacing mode (?(DEFINE) # Within this DEFINE block, we'll define many subroutines # They build on each other like lego until we can define # a "big number" (?<one_to_9> # The basic regex: # one|two|three|four|five|six|seven|eight|nine # We'll use an optimized version: # Option 1: four|eight|(?:fiv|(?:ni|o)n)e|t(?:wo|hree)| # s(?:ix|even) # Option 2: (?:f(?:ive|our)|s(?:even|ix)|t(?:hree|wo)|(?:ni|o)ne|eight) ) # end one_to_9 definition (?<ten_to_19> # The basic regex: # ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen| # eighteen|nineteen # We'll use an optimized version: # Option 1: twelve|(?:(?:elev|t)e|(?:fif|eigh|nine|(?:thi|fou)r| # s(?:ix|even))tee)n # Option 2: (?:(?:(?:s(?:even|ix)|f(?:our|if)|nine)te|e(?:ighte|lev))en| t(?:(?:hirte)?en|welve)) ) # end ten_to_19 definition (?<two_digit_prefix> # The basic regex: # twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety # We'll use an optimized version: # Option 1: (?:fif|six|eigh|nine|(?:tw|sev)en|(?:thi|fo)r)ty # Option 2: (?:s(?:even|ix)|t(?:hir|wen)|f(?:if|or)|eigh|nine)ty ) # end two_digit_prefix definition (?<one_to_99> (?&two_digit_prefix)(?:[- ](?&one_to_9))?|(?&ten_to_19)| (?&one_to_9) ) # end one_to_99 definition (?<one_to_999> (?&one_to_9)[ ]hundred(?:[ ](?:and[ ])?(?&one_to_99))?| (?&one_to_99) ) # end one_to_999 definition (?<one_to_999_999> (?&one_to_999)[ ]thousand(?:[ ](?&one_to_999))?| (?&one_to_999) ) # end one_to_999_999 definition (?<one_to_999_999_999> (?&one_to_999)[ ]million(?:[ ](?&one_to_999_999))?| (?&one_to_999_999) ) # end one_to_999_999_999 definition (?<one_to_999_999_999_999> (?&one_to_999)[ ]billion(?:[ ](?&one_to_999_999_999))?| (?&one_to_999_999_999) ) # end one_to_999_999_999_999 definition (?<one_to_999_999_999_999_999> (?&one_to_999)[ ]trillion(?:[ ](?&one_to_999_999_999_999))?| (?&one_to_999_999_999_999) ) # end one_to_999_999_999_999_999 definition # ==== MORE ==== (?<one_to_quadrillion> (?&one_to_999)[ ]quadrillion(?:[ ](?&one_to_999_999_999_999_999))?| (?&one_to_999_999_999_999_999) ) # end one_to_quadrillion definition (?<one_to_quintillion> (?&one_to_999)[ ]quintillion(?:[ ](?&one_to_quadrillion))?| (?&one_to_quadrillion) ) # end one_to_quintillion definition (?<one_to_sextillion> (?&one_to_999)[ ]sextillion(?:[ ](?&one_to_quintillion))?| (?&one_to_quintillion) ) # end one_to_sextillion definition (?<one_to_septillion> (?&one_to_999)[ ]septillion(?:[ ](?&one_to_sextillion))?| (?&one_to_sextillion) ) # end one_to_septillion definition (?<one_to_octillion> (?&one_to_999)[ ]octillion(?:[ ](?&one_to_septillion))?| (?&one_to_septillion) ) # end one_to_octillion definition (?<one_to_nonillion> (?&one_to_999)[ ]nonillion(?:[ ](?&one_to_octillion))?| (?&one_to_octillion) ) # end one_to_nonillion definition (?<one_to_decillion> (?&one_to_999)[ ]decillion(?:[ ](?&one_to_nonillion))?| (?&one_to_nonillion) ) # end one_to_decillion definition (?<bignumber> zero|(?&one_to_decillion) ) # end bignumber definition (?<zero_to_9> (?&one_to_9)|zero ) # end zero to 9 definition # (?<decimals> # point(?:[ ](?&zero_to_9))+ # ) # end decimals definition ) # End DEFINE ####### The Regex Matching Starts Here ######## \b(?:(?&ten_to_19)\s+hundred|(?&bignumber))\b REGEX; return preg_replace_callback('~' . trim($reg) . '~i', function ($x) { return text2num_internal($x[0]); }, $s); } function text2num_internal($s) { // Port of https://github.com/ghewgill/text2num/blob/master/text2num.py $Small = [ 'zero'=> 0, 'one'=> 1, 'two'=> 2, 'three'=> 3, 'four'=> 4, 'five'=> 5, 'six'=> 6, 'seven'=> 7, 'eight'=> 8, 'nine'=> 9, 'ten'=> 10, 'eleven'=> 11, 'twelve'=> 12, 'thirteen'=> 13, 'fourteen'=> 14, 'fifteen'=> 15, 'sixteen'=> 16, 'seventeen'=> 17, 'eighteen'=> 18, 'nineteen'=> 19, 'twenty'=> 20, 'thirty'=> 30, 'forty'=> 40, 'fifty'=> 50, 'sixty'=> 60, 'seventy'=> 70, 'eighty'=> 80, 'ninety'=> 90 ]; $Magnitude = [ 'thousand'=> 1000, 'million'=> 1000000, 'billion'=> 1000000000, 'trillion'=> 1000000000000, 'quadrillion'=> 1000000000000000, 'quintillion'=> 1000000000000000000, 'sextillion'=> 1000000000000000000000, 'septillion'=> 1000000000000000000000000, 'octillion'=> 1000000000000000000000000000, 'nonillion'=> 1000000000000000000000000000000, 'decillion'=> 1000000000000000000000000000000000, ]; $a = preg_split("~[\s-]+(?:and[\s-]+)?~u", $s); $a = array_map('strtolower', $a); $n = 0; $g = 0; foreach ($a as $w) { # for w in a if (isset($Small[$w])) { $g = $g + $Small[$w]; } else if ($w == "hundred" && $g != 0) { $g = $g * 100; } else { $x = $Magnitude[$w]; if (strlen($x) > 0) { $n =$n + $g * $x; $g = 0; } else{ throw new Exception("Unknown number: " . $w); } } } return $n + $g; } echo text2num("one") . "\n"; // 1 echo text2num("twelve") . "\n"; // 12 echo text2num("seventy two") . "\n"; // 72 echo text2num("three hundred") . "\n"; // 300 echo text2num("twelve hundred") . "\n"; // 1200 echo text2num("twelve thousand three hundred four") . "\n"; // 12304 echo text2num("six million") . "\n"; // 6000000 echo text2num("six million four hundred thousand five") . "\n"; // 6400005 echo text2num("one hundred twenty three billion four hundred fifty six million seven hundred eighty nine thousand twelve") . "\n"; # // 123456789012 echo text2num("four decillion") . "\n"; // 4000000000000000000000000000000000 echo text2num("five hundred and thirty-seven") . "\n"; // 537 echo text2num("five hundred and thirty seven") . "\n"; // 537

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.4.120.0130.00924.37
8.4.110.0130.00819.09
8.4.100.0110.01118.27
8.4.90.0110.01120.79
8.4.80.0140.00919.23
8.4.70.0050.00424.21
8.4.60.0120.01020.46
8.4.50.0130.00718.90
8.4.40.0140.00719.63
8.4.30.0100.01017.77
8.4.20.0140.00320.45
8.4.10.0090.00019.95
8.3.250.0100.01119.07
8.3.240.0110.01017.38
8.3.230.0110.00917.01
8.3.220.0090.01119.06
8.3.210.0140.00917.01
8.3.200.0040.00516.89
8.3.190.0060.00218.98
8.3.180.0120.00819.18
8.3.170.0120.00317.30
8.3.160.0130.00718.64
8.3.150.0170.00321.05
8.3.140.0030.00617.73
8.3.130.0040.00418.48
8.3.120.0030.00619.28
8.3.110.0110.00020.94
8.3.100.0160.00318.55
8.3.90.0050.00526.77
8.3.80.0070.00317.13
8.3.70.0170.00317.00
8.3.60.0080.00818.56
8.3.50.0140.00417.80
8.3.40.0120.00319.47
8.3.30.0120.00419.02
8.3.20.0080.00021.85
8.3.10.0090.00019.06
8.3.00.0040.00419.76
8.2.290.0100.00917.23
8.2.280.0130.00818.73
8.2.270.0120.00816.95
8.2.260.0150.00417.67
8.2.250.0080.00017.23
8.2.240.0100.01019.11
8.2.230.0030.00622.58
8.2.220.0090.00037.54
8.2.210.0080.00026.77
8.2.200.0090.00018.54
8.2.190.0000.01517.13
8.2.180.0070.01125.92
8.2.170.0130.00619.34
8.2.160.0000.01422.96
8.2.150.0050.00324.18
8.2.140.0060.00324.66
8.2.130.0030.00626.16
8.2.120.0060.00321.13
8.2.110.0080.00421.21
8.2.100.0080.00418.29
8.2.90.0090.00018.41
8.2.80.0030.00619.25
8.2.70.0100.00017.88
8.2.60.0090.00318.18
8.2.50.0030.00618.07
8.2.40.0080.00018.60
8.2.30.0000.00818.35
8.2.20.0050.00318.36
8.2.10.0000.00919.76
8.2.00.0040.00418.27
8.1.330.0120.00822.30
8.1.320.0080.01218.28
8.1.310.0030.01616.74
8.1.300.0100.00620.47
8.1.290.0030.00730.84
8.1.280.0000.01525.92
8.1.270.0060.00922.44
8.1.260.0030.00626.35
8.1.250.0080.00028.09
8.1.240.0030.00719.51
8.1.230.0090.00321.16
8.1.220.0060.00318.04
8.1.210.0000.00820.26
8.1.200.0070.00317.85
8.1.190.0000.00817.72
8.1.180.0040.00418.10
8.1.170.0000.00819.09
8.1.160.0050.00319.04
8.1.150.0040.00419.23
8.1.140.0000.00720.38
8.1.130.0040.00420.50
8.1.120.0000.00817.83
8.1.110.0030.00617.77
8.1.100.0030.00517.76
8.1.90.0040.00717.82
8.1.80.0040.00417.72
8.1.70.0000.00817.84
8.1.60.0000.01017.91
8.1.50.0000.00817.88
8.1.40.0000.00917.68
8.1.30.0000.00818.02
8.1.20.0030.00618.00
8.1.10.0100.00017.91
8.1.00.0000.00817.86
8.0.300.0000.00820.36
8.0.290.0050.00316.88
8.0.280.0000.00818.67
8.0.270.0040.00417.43
8.0.260.0050.00220.31
8.0.250.0040.00417.25
8.0.240.0040.00417.26
8.0.230.0040.00417.32
8.0.220.0000.00717.32
8.0.210.0000.00717.28
8.0.200.0040.00417.32
8.0.190.0040.00417.23
8.0.180.0050.00317.32
8.0.170.0030.00617.20
8.0.160.0040.00717.36
8.0.150.0000.00817.33
8.0.140.0000.00817.09
8.0.130.0020.00513.69
8.0.120.0000.00817.39
8.0.110.0090.00017.37
8.0.100.0040.00417.18
8.0.90.0060.00317.43
8.0.80.0100.01317.42
8.0.70.0050.00317.41
8.0.60.0040.00417.34
8.0.50.0060.00317.39
8.0.30.0160.00417.47
8.0.20.0180.00417.41
8.0.10.0030.00517.34
8.0.00.0110.00917.04
7.4.330.0000.00515.55
7.4.320.0040.00416.88
7.4.300.0000.00817.03
7.4.290.0000.00816.96
7.4.280.0030.00517.01
7.4.270.0030.00717.07
7.4.260.0040.00416.99
7.4.250.0040.00416.83
7.4.240.0040.00416.99
7.4.230.0000.00816.81
7.4.220.0000.00816.84
7.4.210.0060.01317.00
7.4.200.0040.00417.03
7.4.160.0090.01016.88
7.4.140.0120.00917.86
7.4.130.0070.01117.11
7.4.120.0060.01216.78
7.4.110.0100.01017.10
7.4.100.0120.00617.11
7.4.90.0040.01416.88
7.4.80.0140.00519.39
7.4.70.0060.01317.07
7.4.60.0100.01016.74
7.4.50.0090.00516.75
7.4.40.0070.01316.93
7.4.10.0100.01015.29
7.4.00.0080.00915.27
7.3.330.0000.00613.52
7.3.320.0030.00313.81
7.3.310.0050.00316.67
7.3.300.0040.00416.64
7.3.290.0030.00516.78
7.3.280.0060.01016.76
7.3.260.0110.01116.79
7.3.240.0120.00716.87
7.3.230.0070.01116.89
7.3.210.0070.01116.80
7.3.200.0080.01216.91
7.3.190.0110.00716.80
7.3.180.0140.01016.80
7.3.170.0060.01316.77
7.3.160.0090.00916.85
7.3.130.0050.01415.16
7.3.120.0080.00915.18
7.3.110.0070.01015.24
7.3.100.0040.01115.30
7.3.90.0070.01015.22
7.3.80.0060.00615.09
7.3.70.0080.00715.28
7.3.60.0080.00615.14
7.3.50.0070.00615.22
7.3.40.0100.00715.03
7.3.30.0070.00915.10
7.3.20.0080.00717.03
7.3.10.0080.00516.91
7.3.00.0030.01116.82
7.2.330.0060.01217.03
7.2.320.0060.01616.95
7.2.310.0100.01016.84
7.2.300.0040.01517.05
7.2.290.0090.01517.13
7.2.260.0080.01215.41
7.2.250.0040.01515.39
7.2.240.0060.01115.41
7.2.230.0060.01115.45
7.2.220.0050.01015.47
7.2.210.0060.00715.44
7.2.200.0080.00515.53
7.2.190.0050.00915.45
7.2.180.0100.00415.32
7.2.170.0100.00715.24
7.2.160.0060.00915.29
7.2.150.0070.00817.12
7.2.140.0070.00917.06
7.2.130.0060.00817.07
7.2.120.0100.00417.16
7.2.110.0060.00917.09
7.2.100.0060.00717.15
7.2.90.0080.00617.15
7.2.80.0050.01717.20
7.2.70.0030.01117.13
7.2.60.0070.00517.17
7.2.50.0020.01617.27
7.2.40.0100.00617.14
7.2.30.0090.00417.21
7.2.20.0090.00917.25
7.2.10.0090.00617.18
7.2.00.0050.01017.17
7.1.330.0060.00916.08
7.1.320.0090.00816.07
7.1.310.0100.00816.24
7.1.300.0060.00915.93
7.1.290.0070.00716.08
7.1.280.0110.00315.96
7.1.270.0080.00616.06
7.1.260.0050.00916.03
7.1.250.0070.00716.05
7.1.240.0040.00816.06
7.1.230.0050.00915.93
7.1.220.0100.00515.92
7.1.210.0080.00716.09
7.1.200.0050.01216.04
7.1.190.0050.01116.13
7.1.180.0030.00916.11
7.1.170.0060.00915.96
7.1.160.0040.00716.00
7.1.150.0060.00815.98
7.1.140.0050.00915.94
7.1.130.0040.01216.15
7.1.120.0060.00916.17
7.1.110.0060.00616.19
7.1.100.0050.00916.15
7.1.90.0020.01216.06
7.1.80.0060.00716.17
7.1.70.0070.00316.06
7.1.60.0020.01015.96
7.1.50.0090.00315.99
7.1.40.0100.00615.88
7.1.30.0050.01216.08
7.1.20.0030.01116.00
7.1.10.0080.00615.89
7.1.00.0050.00916.04

preferences:
121.82 ms | 403 KiB | 5 Q