3v4l.org

run code in 500+ 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;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Cr9T3
function name:  (null)
number of ops:  24
compiled vars:  !0 = $buffer, !1 = $inFormat, !2 = $outFormat, !3 = $bufferWithFarsiNumbers
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    4     0  E >   ASSIGN                                                       !0, 'it+need+to+only+select+%2C+when+there+is+number+around+it.+for+example+only+%0Awhen+123%2C456+i+need+to+select+and+replace+%22%2C%22+I%27m+converting+English%0Anumbers+into+Persian+%28e.g%3A+%22Hello+123%22+becomes+%22Hello+%DB%B1%DB%B2%DB%B3%22%29.+now+I+need+to%0Areplace+the+Decimal+separator+with+Persian+version+too.+but+I+don%27t+know+how%0AI+can+select+it+with+regex.+e.g%3A+%22Hello+121%2C534%22+most+become+%0A%22Hello+%DB%B1%DB%B2%DB%B1%2F%DB%B5%DB%B3%DB%B4%22+The+character+that+needs+to+be+replaced+is+%2C+with+%2F'
   14     1        NEW                                                  $5      'NumberFormatter'
          2        SEND_VAL_EX                                                  'en_UK'
          3        FETCH_CLASS_CONSTANT                                 ~6      'NumberFormatter', 'DECIMAL'
          4        SEND_VAL_EX                                                  ~6
          5        DO_FCALL                                          0          
          6        ASSIGN                                                       !1, $5
   15     7        NEW                                                  $9      'NumberFormatter'
          8        SEND_VAL_EX                                                  'fa_IR'
          9        FETCH_CLASS_CONSTANT                                 ~10     'NumberFormatter', 'DECIMAL'
         10        SEND_VAL_EX                                                  ~10
         11        DO_FCALL                                          0          
         12        ASSIGN                                                       !2, $9
   17    13        INIT_FCALL                                                   'preg_replace_callback'
   18    14        SEND_VAL                                                     '%28%5Cb%5B1-9%5D%5Cd%7B0%2C2%7D%28%3F%3A%5B+%2C.%5D%5Cd%7B3%7D%29%2A%5Cb%29u'
   19    15        DECLARE_LAMBDA_FUNCTION                              ~13     [0]
         16        BIND_LEXICAL                                                 ~13, !1
         17        BIND_LEXICAL                                                 ~13, !2
   28    18        SEND_VAL                                                     ~13
   29    19        SEND_VAR                                                     !0
   17    20        DO_ICALL                                             $14     
         21        ASSIGN                                                       !3, $14
   32    22        ECHO                                                         !3
   33    23      > RETURN                                                       1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 14
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Cr9T3
function name:  {closure:/in/Cr9T3:19}
number of ops:  25
compiled vars:  !0 = $matches, !1 = $inFormat, !2 = $outFormat, !3 = $number, !4 = $result
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   19     0  E >   RECV                                                 !0      
          1        BIND_STATIC                                                  !1
          2        BIND_STATIC                                                  !2
   20     3        QM_ASSIGN                                            ~5      !0
          4        FETCH_LIST_R                                         $6      ~5, 0
          5        ASSIGN                                                       !3, $6
          6        FREE                                                         ~5
   22     7        INIT_METHOD_CALL                                             !1, 'parse'
          8        SEND_VAR_EX                                                  !3
          9        DO_FCALL                                          0  $8      
         10        ASSIGN                                                       !4, $8
   23    11        TYPE_CHECK                                        4          !4
         12      > JMPZ                                                         ~10, ->14
   24    13    > > RETURN                                                       !3
   27    14    >   INIT_FCALL                                                   'sprintf'
         15        SEND_VAL                                                     '%3C+%25s+%28%25.4f%29+%3D+%25s+%3E'
         16        SEND_VAR                                                     !3
         17        SEND_VAR                                                     !4
         18        INIT_METHOD_CALL                                             !2, 'format'
         19        SEND_VAR_EX                                                  !4
         20        DO_FCALL                                          0  $11     
         21        SEND_VAR                                                     $11
         22        DO_ICALL                                             $12     
         23      > RETURN                                                       $12
   28    24*     > RETURN                                                       null

End of Dynamic Function 0

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
177.37 ms | 2246 KiB | 15 Q