3v4l.org

run code in 300+ PHP versions simultaneously
<?php # some text $buffer = <<<TEXT it need to only select , when there is number around it. for example only when 123,456 i need to select and replace "," I'm converting English numbers into Persian (e.g: "Hello 123" becomes "Hello ۱۲۳"). now I need to replace the Decimal separator with Persian version too. but I don't know how I can select it with regex. e.g: "Hello 121,534" most become "Hello ۱۲۱/۵۳۴" The character that needs to be replaced is , with / TEXT; # prepare formatters $inFormat = new NumberFormatter('en_UK', NumberFormatter::DECIMAL); $outFormat = new NumberFormatter('fa_IR', NumberFormatter::DECIMAL); $bufferWithFarsiNumbers = preg_replace_callback( '(\b[1-9]\d{0,2}(?:[ ,.]\d{3})*\b)u', function (array $matches) use ($inFormat, $outFormat) { [$number] = $matches; $result = $inFormat->parse($number); if (false === $result) { return $number; } return sprintf("< %s (%.4f) = %s >", $number, $result, $outFormat->format($result)); }, $buffer ); echo $bufferWithFarsiNumbers;
Output for 7.1.25 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.32, 8.0.0 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
it need to only select , when there is number around it. for example only when < 123,456 (123456.0000) = ۱۲۳٬۴۵۶ > i need to select and replace "," I'm converting English numbers into Persian (e.g: "Hello < 123 (123.0000) = ۱۲۳ >" becomes "Hello ۱۲۳"). now I need to replace the Decimal separator with Persian version too. but I don't know how I can select it with regex. e.g: "Hello < 121,534 (121534.0000) = ۱۲۱٬۵۳۴ >" most become "Hello ۱۲۱/۵۳۴" The character that needs to be replaced is , with /
Output for 8.0.13
Fatal error: Uncaught Error: Class "NumberFormatter" not found in /in/Cr9T3:14 Stack trace: #0 {main} thrown in /in/Cr9T3 on line 14
Process exited with code 255.
Output for 7.3.32 - 7.3.33, 7.4.33
Fatal error: Uncaught Error: Class 'NumberFormatter' not found in /in/Cr9T3:14 Stack trace: #0 {main} thrown in /in/Cr9T3 on line 14
Process exited with code 255.

preferences:
188.48 ms | 401 KiB | 183 Q