為什么我加了括號就輸出2個了?
<?php
$subject = "my email is [email protected]";
//在這里補充代碼,實現正則匹配,并輸出郵箱地址
$patten = "/(\w+@\w+.com)/";
preg_match($patten,$subject,$matches);
print_r($matches);
Array
(
[0] => [email protected]
[1] => [email protected]
)
<?php
$subject = "my email is [email protected]";
//在這里補充代碼,實現正則匹配,并輸出郵箱地址
$patten = "/(\w+@\w+.com)/";
preg_match($patten,$subject,$matches);
print_r($matches);
Array
(
[0] => [email protected]
[1] => [email protected]
)
2015-10-24
舉報
2015-10-24
preg_match 函數本身定義所決定。第一次是全匹配,第二次是第一個子模式匹配結果
int preg_match ? ?( string $pattern ? , string $subject ? [, array &$matches ? [, int $flags = 0 ? [, int $offset = 0 ?]]] )
搜索subject與pattern給定的正則表達式的一個匹配.?
參數
pattern
要搜索的模式,字符串類型。
subject
輸入字符串。
matches
如果提供了參數matches,它將被填充為搜索結果。$matches[0]將包含完整模式匹配到的文本,$matches[1]將包含第一個捕獲子組匹配到的文本,以此類推。
flags
flags可以被設置為以下標記值:
PREG_OFFSET_CAPTURE
? ? ? ? ?如果傳遞了這個標記,對于每一個出現的匹配返回時會附加字符串偏移量(相對于目標字符串的)。 ? ? ? ? ?注意:這會改變填充到matches參數的數組,使其每個元素成為一個由 ? ? ? ? ?第0個元素是匹配到的字符串,第1個元素是該匹配字符串 ? ? ? ? ?在目標字符串subject中的偏移量。 ? ? ? ? ?
offset
通常,搜索從目標字符串的開始位置開始??蛇x參數 offset 用于 ? ? ?指定從目標字符串的某個未知開始搜索(單位是字節)。
返回值
preg_match()返回 pattern 的匹配次數。 ?它的值將是0次(不匹配)或1次,因為preg_match()在第一次匹配后 ?將會停止搜索。