php正则匹配指定字符串,获取截取指定内容,preg |
您所在的位置:网站首页 › php截取网页指定内容的方法 › php正则匹配指定字符串,获取截取指定内容,preg |
一、匹配简单的 (.*?) //匹配AAA和BBB之间的内容 preg_match('/AAA(.*?)BBB/', $fcontents, $matches); $uu=$matches[1]; //匹配多个字符串 preg_match('/AAA(.*?)BBB/', $fcontents, $matches); preg_match('/CCC(.*?)DDD/', $fcontents, $matches2); $uu=$matches[1]; $kk=$matches2[1];二、匹配复杂的 ([^"]*?) 多个相同的开头,匹配最后一个 //匹配AAA和BBB之间的内容 preg_match('/AAA([^"]*?)BBB/', $fcontents, $matches); $uu=$matches[1]; //匹配多个字符串 preg_match('/AAA([^"]*?)BBB/', $fcontents, $matches); preg_match('/CCC([^"]*?)DDD/', $fcontents, $matches2); $uu=$matches[1]; $kk=$matches2[1];【备注1】如果正则匹配里含有变量,变量用’.$aaa.'来表示,方法如下: preg_match('/AA'.$star.'AA([^"]*?)BB'.$stop.'BB/', $fcontents, $matches);【备注2】如果匹配内容前有未知的变化参数,用 .*?来替代,如下: preg_match('/AAA.*?BBB(.*?)CCC/', $fcontents, $matches);【备注3】正则匹配的/u,/i,/s的含义 /u 表示按unicode(utf-8)匹配(主要针对多字节比如汉字) /i 表示不区分大小写(如果表达式里面有 a, 那么 A 也是匹配对象) /s 表示将字符串视为单行来匹配 可以混用,比如 preg_match(’/AAA.?BBB(.?)CCC/sui’, $fcontents, $matches); |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |