php正则匹配指定字符串,截取指定内容,preg_match使用实例

一、匹配简单的 (.*?)

//匹配AAA和BBB之间的内容
$isMatched = preg_match('/AAA(.*?)BBB/', $fcontents, $matches);
$uu=$matches[1];
//匹配多个字符串
$isMatched = preg_match('/AAA(.*?)BBB/', $fcontents, $matches);
$isMatched = preg_match('/CCC(.*?)DDD/', $fcontents, $matches2);
$uu=$matches[1];
$kk=$matches2[1];

二、匹配复杂的 ([^"]*?) 多个相同的开头,匹配最后一个

//匹配AAA和BBB之间的内容
$isMatched = preg_match('/AAA([^"]*?)BBB/', $fcontents, $matches);
$uu=$matches[1];
//匹配多个字符串
$isMatched = preg_match('/AAA([^"]*?)BBB/', $fcontents, $matches);
$isMatched = preg_match('/CCC([^"]*?)DDD/', $fcontents, $matches2);
$uu=$matches[1];
$kk=$matches2[1];

【备注】如果正则匹配里含有变量,变量用’.$aaa.'来表示,方法如下:

$isMatched = preg_match('/AA'.$star.'AA([^"]*?)BB'.$stop.'BB/', $fcontents, $matches);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章