3v4l.org

run code in 300+ PHP versions simultaneously
<?php $rows = [ ['`1234567890-=', '~!@#$%^&*()_+'], ['qwertyuiop[]\\', 'QWERTYUIOP{}|'], ['asdfghjkl;\'', 'ASDFGHJKL:"'], ['zxcvbnm,./', 'ZXCVBNM<>?'] ]; $wholeregex = []; foreach($rows as $k => $row) { $num = $k + 1; echo "Row $num\n"; $array = []; foreach($row as $subrow) { $subarray = array_map('ord', str_split($subrow)); $array = array_merge($subarray, $array); } sort($array); echo "\t" . implode( ' ', $array), "\n"; $ret = [$array[0]]; for($i = 1; $i < count($array); $i++) { $num = $array[$i]; if(($array[$i - 1] + 1) == $num) { if( ($i == (count($array) - 1)) || ($array[$i + 1] != ($num + 1)) ) { $ret[] = array_pop($ret) . '-' . $num; } } else { $ret[] = $num; } } echo "\t" . implode( ' ', $ret), "\n"; $regex = '(['; $subregex = implode( '', array_map(function($el) { $vals = explode('-', $el); if(count($vals) == 1) { return chr($vals[0]); } return chr($vals[0]) . '-' . chr($vals[1]); }, $ret)); $regex .= $subregex; $regex .= '])'; echo "\t" . $regex, "\n"; $wholeregex[] = $regex; } $finalregex = '%' . implode('|', $wholeregex) . '%'; $finalregex = str_replace('(-', '\(-', $finalregex); echo $finalregex, "\n"; echo strlen($finalregex), "\n"; $input = "a"; preg_match($finalregex, $input, $m); echo array_flip($m)[$input];
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Row 1 33 35 36 37 38 40 41 42 43 45 48 49 50 51 52 53 54 55 56 57 61 64 94 95 96 126 33 35-38 40-43 45 48-57 61 64 94-96 126 ([!#-&(-+-0-9=@^-`~]) Row 2 69 73 79 80 81 82 84 85 87 89 91 92 93 101 105 111 112 113 114 116 117 119 121 123 124 125 69 73 79-82 84-85 87 89 91-93 101 105 111-114 116-117 119 121 123-125 ([EIO-RT-UWY[-]eio-rt-uwy{-}]) Row 3 34 39 58 59 65 68 70 71 72 74 75 76 83 97 100 102 103 104 106 107 108 115 34 39 58-59 65 68 70-72 74-76 83 97 100 102-104 106-108 115 (["':-;ADF-HJ-LSadf-hj-ls]) Row 4 44 46 47 60 62 63 66 67 77 78 86 88 90 98 99 109 110 118 120 122 44 46-47 60 62-63 66-67 77-78 86 88 90 98-99 109-110 118 120 122 ([,.-/<>-?B-CM-NVXZb-cm-nvxz]) %([!#-&\(-+-0-9=@^-`~])|([EIO-RT-UWY[-]eio-rt-uwy{-}])|(["':-;ADF-HJ-LSadf-hj-ls])|([,.-/<>-?B-CM-NVXZb-cm-nvxz])% 114 3

preferences:
156.79 ms | 406 KiB | 197 Q