當前位置: 首頁> 函數類別大全> preg_match_all

preg_match_all

執行全局正則表達式匹配
名稱:preg_match_all
分類:正則處理PCRE
所屬語言:php
一句話介紹:在字符串中查找模式的所有匹配項。

定義和用法

preg_match_all()函數返回在字符串中找到的模式的匹配數,並用找到的匹配填充變量。

實例

例子1

查找字符串中所有"ain" 的出現:

 <?php
$str = "The rain in SPAIN falls mainly on the plains." ;
$pattern = "/ain/i" ;
if ( preg_match_all ( $pattern , $str , $matches ) ) {
  print_r ( $matches ) ;
}
?>

親自試一試

例子2

使用PREG_PATTERN_ORDER 設置matches 數組的結構。在此例中,matches 數組中的每個元素都有正則表達式分組之一的所有匹配項。

 <?php
$str = "abc ABC" ;
$pattern = "/((a)b)(c)/i" ;
if ( preg_match_all ( $pattern , $str , $matches , PREG_PATTERN_ORDER ) ) {
  print_r ( $matches ) ;
}
?>

親自試一試

同類函數