3v4l.org

run code in 300+ PHP versions simultaneously
<?php function parse(string $input): array { static $pattern = <<<'EOD' / # 先頭 ^ # name 部分 (?<name> # \p{L}\p{N} を最低1個は含む (?=[^「」\p{C}\p{L}\p{N}]*+[\p{L}\p{N}]) # カッコと制御文字以外の繰り返し [^「」\p{C}]++ )? # カッコ+空白だけのコメントの場合は読み飛ばす条件分岐 (?: # empty_comment 部分 (?<empty_comment> 「 # カッコの中身 (?: # empty_comment の再帰 (?&empty_comment) )*+ 」 ) # empty_comment がマッチしたらそこまでをすべて読み飛ばす (*SKIP)(*FAIL) | # comment 部分 (?<comment> 「 # カッコの中身 (?: # \p{L}\p{N} を最低1個は含む (?=[^「」\p{C}\p{L}\p{N}]*+[\p{L}\p{N}]) # カッコと制御文字以外の繰り返し [^「」\p{C}]*+ # または | # comment の再帰 (?&comment) )*+ 」 )? ) # 末尾 $ /ux EOD; preg_match($pattern, $input, $match); $name = $match['name'] ?? ''; $comment = $match['comment'] ?? ''; // 外側のカッコを切り取り $comment = mb_substr($comment, 1, -1, 'UTF-8'); $name = $name === '' ? null : $name; $comment = $comment === '' ? null : $comment; return compact('name', 'comment'); } $inputs = [ /* * 正しい値が与えられた場合、一方か両方に値が入る *----------------------------------------------*/ '一郎「こんにちは」', // ["name"=>"一郎","comment"=>"こんにちは"] '二郎「男の中の「男」です」', // ["name"=>"二郎","comment"=>"男の中の「男」です"] '三郎', // ["name"=>"三郎","comment"=>"null"] '「こんばんは」', // ["name"=>"null","comment"=>"こんばんは"] /* * 不正な値が与えられた場合、いずれも null を返す *----------------------------------------------*/ //(1)以下のように「」の後に文字がある場合 null とする '「こんばんは」です', // ["name"=>"null","comment"=>"null"] //(2)以下のように「」がセットじゃない場合 null とする '五郎「男の中の「男」かも', // ["name"=>"null","comment"=>"null"] '「男の中の「男」かも', // 同上 '「やばい', // 同上 'ねむい」', // 同上 '「', // 同上 '」', // 同上 '「「」', // 同上 //(3)以下のように記号だけの場合 null とする '「」', // ["name"=>"null","comment"=>"null"] '「「」」', // 同上 '「★」', // 同上 '「!!」', // 同上 ]; foreach ($inputs as $input) { var_dump([$input => parse($input)]); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 11
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 11
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/6REYe
function name:  (null)
number of ops:  13
compiled vars:  !0 = $inputs, !1 = $input
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E >   ASSIGN                                                   !0, <array>
  114     1      > FE_RESET_R                                       $3      !0, ->11
          2    > > FE_FETCH_R                                               $3, !1, ->11
  115     3    >   INIT_FCALL                                               'var_dump'
          4        INIT_FCALL                                               'parse'
          5        SEND_VAR                                                 !1
          6        DO_FCALL                                      0  $4      
          7        INIT_ARRAY                                       ~5      $4, !1
          8        SEND_VAL                                                 ~5
          9        DO_ICALL                                                 
  114    10      > JMP                                                      ->2
         11    >   FE_FREE                                                  $3
  116    12      > RETURN                                                   1

Function parse:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 26
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 32
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 32
Branch analysis from position: 30
Branch analysis from position: 32
filename:       /in/6REYe
function name:  parse
number of ops:  42
compiled vars:  !0 = $input, !1 = $pattern, !2 = $match, !3 = $name, !4 = $comment
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        BIND_STATIC                                              !1
   65     2        INIT_FCALL                                               'preg_match'
          3        SEND_VAR                                                 !1
          4        SEND_VAR                                                 !0
          5        SEND_REF                                                 !2
          6        DO_ICALL                                                 
   67     7        FETCH_DIM_IS                                     ~6      !2, 'name'
          8        COALESCE                                         ~7      ~6
          9        QM_ASSIGN                                        ~7      ''
         10        ASSIGN                                                   !3, ~7
   68    11        FETCH_DIM_IS                                     ~9      !2, 'comment'
         12        COALESCE                                         ~10     ~9
         13        QM_ASSIGN                                        ~10     ''
         14        ASSIGN                                                   !4, ~10
   71    15        INIT_FCALL                                               'mb_substr'
         16        SEND_VAR                                                 !4
         17        SEND_VAL                                                 1
         18        SEND_VAL                                                 -1
         19        SEND_VAL                                                 'UTF-8'
         20        DO_ICALL                                         $12     
         21        ASSIGN                                                   !4, $12
   73    22        IS_IDENTICAL                                             !3, ''
         23      > JMPZ                                                     ~14, ->26
         24    >   QM_ASSIGN                                        ~15     null
         25      > JMP                                                      ->27
         26    >   QM_ASSIGN                                        ~15     !3
         27    >   ASSIGN                                                   !3, ~15
   74    28        IS_IDENTICAL                                             !4, ''
         29      > JMPZ                                                     ~17, ->32
         30    >   QM_ASSIGN                                        ~18     null
         31      > JMP                                                      ->33
         32    >   QM_ASSIGN                                        ~18     !4
         33    >   ASSIGN                                                   !4, ~18
   76    34        INIT_FCALL                                               'compact'
         35        SEND_VAL                                                 'name'
         36        SEND_VAL                                                 'comment'
         37        DO_ICALL                                         $20     
         38        VERIFY_RETURN_TYPE                                       $20
         39      > RETURN                                                   $20
   77    40*       VERIFY_RETURN_TYPE                                       
         41*     > RETURN                                                   null

End of function parse

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
145.08 ms | 963 KiB | 18 Q