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;

preferences:
32.57 ms | 402 KiB | 5 Q