1、需求
如下内容,获取JSESSIONID后面的6F2FB1EA915913DEF3256D11101F1DF3
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.
account.chsi.com.cnFALSE/passportFALSE0JSESSIONID6F2FB1EA915913DEF3256D11101F1DF3
2、实现
通过preg_match实现
$content = "# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.
account.chsi.com.cnFALSE/passportFALSE0JSESSIONID6F2FB1EA915913DEF3256D11101F1DF3";
$pattern = "/JSESSIONID([0-9A-Z]+)/i";
$match = preg_match($pattern,$content,$matches);
var_dump($matches);
var_dump($match);
?>
3、延伸
1)$pattern = "/JSESSIONID([0-9A-Z]+)/i";
2)$pattern = "/JSESSIONID([0-9A-Z])+/i";
1)和2)的区别 +的位置在括号内和括号外
$matches[1] 返回的是括号内的内容,所以1)可以返回所有,2)只能返回一个数字