preg_match_all
執行全局正則表達式匹配
preg_match_all()
函數返回在字符串中找到的模式的匹配數,並用找到的匹配填充變量。
查找字符串中所有"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 ) ; } ?>
親自試一試
使用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 ) ; } ?>
親自試一試