3v4l.org

run code in 300+ 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:  33
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        INIT_FCALL                                               'implode'
          8        SEND_VAL                                                 '%7C'
          9        SEND_VAR                                                 !1
         10        DO_ICALL                                         $7      
         11        SEND_VAR_NO_REF_EX                                       $7
         12        DO_FCALL                                      0          
   30    13        ECHO                                                     '%0A'
         14        ECHO                                                     '%0A'
   41    15        INIT_FCALL                                               'array_map'
         16        DECLARE_LAMBDA_FUNCTION                          ~9      [0]
         17        BIND_LEXICAL                                             ~9, !2
         18        SEND_VAL                                                 ~9
         19        SEND_VAR                                                 !1
         20        DO_ICALL                                         $10     
         21        ASSIGN                                                   !3, $10
   44    22        INIT_FCALL_BY_NAME                                       'matchAndShowData'
         23        SEND_VAR_EX                                              !0
         24        SEND_VAR_EX                                              !1
         25        SEND_VAR_EX                                              !2
         26        INIT_FCALL                                               'implode'
         27        SEND_VAL                                                 '%7C'
         28        SEND_VAR                                                 !3
         29        DO_ICALL                                         $12     
         30        SEND_VAR_NO_REF_EX                                       $12
         31        DO_FCALL                                      0          
   57    32      > 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}
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.0.0


preferences:
200.14 ms | 1011 KiB | 18 Q