先看看在php手冊中的說明
int preg_match ( string pattern, string subject [, array matches [, int flags]] )
在 subject 字符串中搜索與 pattern 給出的正則表達式相匹配的內容。
如果提供了 matches,則其會被搜索的結果所填充。$matches[0] 將包含與整個模式匹配的文本,$matches[1] 將包含與第一個捕獲的括號中的子模式所匹配的文本,以此類推。
flags 可以是下列標記:
測試代碼
$str = 'great 1234,This id a page test 3456';
preg_match('/\d\d\d\d/si', $str, $arr);
echo "<pre>";
print_r ($arr);
echo "<pre>";
打印結果:
Array
(
[0] => 1234
)
注意事項
//只會匹配第一個目標,不會匹配第二個目標
//只匹配 1234 不會匹配3456