现在我有了下面的字符串
"IG 449WW 6180262 250"
我想匹配前两个字母,然后是第二个5个字母和数字,然后是7个数字,我想捕获每一个。
所以我有以下preg_match:
$scanned_barcode = trim(Input::get('barcode'));
if (preg_match("/([A-Z]{2})'s('d{3}[A-Z]{2})'s('d{7})/", $scanned_barcode, $found)) {
$mfg_id = $found[1];
$game_code = $found[2];
$serial = Game::find($found[3]);
}
我这样做对吗?我是不是漏掉了什么?有更好的方法吗?
您的正则表达式可以工作,但这样做会更容易:
<?php
$scanned_barcode = trim(Input::get('barcode'));
// 'explode' the string into an array(), using the space as the delimiter
// http://php.net/manual/en/function.explode.php
$found = explode(' ', $scanned_barcode);
$mfg_id = $found[0];
$game_code = $found[1];
$serial = Game::find($found[2]);
?>