3v4l.org

run code in 500+ PHP versions simultaneously
<?php // Sample data $data = [ 'Wellington New Zealand', 'Florida United States of America', 'Quebec Canada', 'Something Country XYZ (formally ABC)', ]; // Array of all possible countries $countries = [ 'United States of America', 'Canada', 'New Zealand', 'Country XYZ (formally ABC)', ]; // The begining and ending pattern delimiter for the RegEx $delim = '/'; /*************************************************** * Option 1 * * Simple but potentially flawed depending on data * **************************************************/ matchAndShowData($data, $countries, $delim, implode('|', $countries)); echo PHP_EOL, PHP_EOL; /******************************* * Option 2 * * Always escape, just in case * ******************************/ // This is an alternative form of the above, and is arguably overkill for this specific // situation, but I always run my code preg_quote, just in case. // This function will ensure that just in case a country has a RegEx modifier that it gets // properly escaped. $patternParts = array_map(fn(string $country) => preg_quote($country, $delim), $countries); // Implode the cleaned countries using the RegEx pipe operator which means "OR" matchAndShowData($data, $countries, $delim, implode('|', $patternParts)); function matchAndShowData(array $data, array $countries, string $delim, string $countryParts): void { $pattern = "^(?<region>.*?) (?<country>$countryParts)$"; foreach($data as $d) { if(preg_match($delim . $pattern . $delim, $d, $matches)){ echo sprintf('%1$s, %2$s', $matches['region'], $matches['country']), PHP_EOL; } else { echo 'NO MATCH: ' . $d, PHP_EOL; } } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CaNRZ
function name:  (null)
number of ops:  27
compiled vars:  !0 = $data, !1 = $countries, !2 = $delim, !3 = $patternParts
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    4     0  E >   ASSIGN                                                       !0, <array>
   12     1        ASSIGN                                                       !1, <array>
   20     2        ASSIGN                                                       !2, '%2F'
   26     3        INIT_FCALL_BY_NAME                                           'matchAndShowData'
          4        SEND_VAR_EX                                                  !0
          5        SEND_VAR_EX                                                  !1
          6        SEND_VAR_EX                                                  !2
          7        FRAMELESS_ICALL_2                implode             ~7      '%7C', !1
          8        SEND_VAL_EX                                                  ~7
          9        DO_FCALL                                          0          
   30    10        ECHO                                                         '%0A'
         11        ECHO                                                         '%0A'
   41    12        INIT_FCALL                                                   'array_map'
         13        DECLARE_LAMBDA_FUNCTION                              ~9      [0]
         14        BIND_LEXICAL                                                 ~9, !2
         15        SEND_VAL                                                     ~9
         16        SEND_VAR                                                     !1
         17        DO_ICALL                                             $10     
         18        ASSIGN                                                       !3, $10
   44    19        INIT_FCALL_BY_NAME                                           'matchAndShowData'
         20        SEND_VAR_EX                                                  !0
         21        SEND_VAR_EX                                                  !1
         22        SEND_VAR_EX                                                  !2
         23        FRAMELESS_ICALL_2                implode             ~12     '%7C', !3
         24        SEND_VAL_EX                                                  ~12
         25        DO_FCALL                                          0          
   57    26      > RETURN                                                       1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CaNRZ
function name:  {closure:/in/CaNRZ:41}
number of ops:  8
compiled vars:  !0 = $country, !1 = $delim
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   41     0  E >   RECV                                                 !0      
          1        BIND_STATIC                                                  !1
          2        INIT_FCALL                                                   'preg_quote'
          3        SEND_VAR                                                     !0
          4        SEND_VAR                                                     !1
          5        DO_ICALL                                             $2      
          6      > RETURN                                                       $2
          7*     > RETURN                                                       null

End of Dynamic Function 0

Function matchandshowdata:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 32
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 32
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 28
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
filename:       /in/CaNRZ
function name:  matchAndShowData
number of ops:  34
compiled vars:  !0 = $data, !1 = $countries, !2 = $delim, !3 = $countryParts, !4 = $pattern, !5 = $d, !6 = $matches
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   46     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV                                                 !2      
          3        RECV                                                 !3      
   48     4        ROPE_INIT                                         3  ~8      '%5E%28%3F%3Cregion%3E.%2A%3F%29+%28%3F%3Ccountry%3E'
          5        ROPE_ADD                                          1  ~8      ~8, !3
          6        ROPE_END                                          2  ~7      ~8, '%29%24'
          7        ASSIGN                                                       !4, ~7
   50     8      > FE_RESET_R                                           $11     !0, ->32
          9    > > FE_FETCH_R                                                   $11, !5, ->32
   51    10    >   INIT_FCALL                                                   'preg_match'
         11        CONCAT                                               ~12     !2, !4
         12        CONCAT                                               ~13     ~12, !2
         13        SEND_VAL                                                     ~13
         14        SEND_VAR                                                     !5
         15        SEND_REF                                                     !6
         16        DO_ICALL                                             $14     
         17      > JMPZ                                                         $14, ->28
   52    18    >   INIT_FCALL                                                   'sprintf'
         19        SEND_VAL                                                     '%251%24s%2C+%252%24s'
         20        FETCH_DIM_R                                          ~15     !6, 'region'
         21        SEND_VAL                                                     ~15
         22        FETCH_DIM_R                                          ~16     !6, 'country'
         23        SEND_VAL                                                     ~16
         24        DO_ICALL                                             $17     
         25        ECHO                                                         $17
         26        ECHO                                                         '%0A'
   51    27      > JMP                                                          ->31
   54    28    >   CONCAT                                               ~18     'NO+MATCH%3A+', !5
         29        ECHO                                                         ~18
         30        ECHO                                                         '%0A'
   50    31    > > JMP                                                          ->9
         32    >   FE_FREE                                                      $11
   57    33      > RETURN                                                       null

End of function matchandshowdata

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
173.27 ms | 1911 KiB | 17 Q