3v4l.org

run code in 300+ PHP versions simultaneously
<?php $strings = ['12345', 'L12345', 'L1S1', 'LS1', 'L123S45', 'L1x23S4yZY5', 'L1x!23S4y5']; foreach($strings as $string) { $hasMatches = preg_match_all('/^L([0-9a-zA-Z]+)S([0-9a-zA-Z]+)/m', $string, $matches, PREG_SET_ORDER, 0); if(filter_var($hasMatches, FILTER_VALIDATE_BOOLEAN)) { // converts $hasMatches to boolean echo 'THERE IS A MATCH: "' . $matches[0][0] . '"' . "\n"; echo '1st part: ' . $matches[0][1] . "\n"; echo '2nd part: ' . $matches[0][2] . "\n\n"; } }
Output for 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.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
THERE IS A MATCH: "L1S1" 1st part: 1 2nd part: 1 THERE IS A MATCH: "L123S45" 1st part: 123 2nd part: 45 THERE IS A MATCH: "L1x23S4yZY5" 1st part: 1x23 2nd part: 4yZY5

preferences:
132.96 ms | 407 KiB | 5 Q