1 回答

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊
您可以執(zhí)行preg_match_all(),其中使用以下正則表達(dá)式需要至少 1 個(gè)(但可能有多個(gè))字符,它是字母 az 或 AZ 或介于 0-9 之間和L
之后S
的數(shù)字S
就像是:
<?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";
? ? }
}
上面的輸出是:
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
- 1 回答
- 0 關(guān)注
- 127 瀏覽
添加回答
舉報(bào)