1 回答
TA贡献1864条经验 获得超2个赞
您可以执行preg_match_all(),其中使用以下正则表达式需要至少 1 个(但可能有多个)字符,它是字母 az 或 AZ 或介于 0-9 之间和L
之后S
的数字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 关注
- 85 浏览
添加回答
举报