求补救匹配php字符串中的cookie
下面这是一段php字符串:
http/1.1 200 ok date: wed, 12 mar 2014 11:57:58 gmt server: apache/2.2.9 (apmserv) mod_ssl/2.2.9 openssl/0.9.8h php/5.2.6 x-powered-by: php/5.2.6 cache-control: public,max-age=10 last-modified: wed, 12 mar 2014 11:57:59 gmt set-cookie: session=cef14157b685078cf9c8f5d66fcefa81116a180d%7e53204bc7209fe349757896; expires=wed, 12-mar-2014 13:57:59 gmt; path=/; domain=.*****.com transfer-encoding: chunked content-type: text/html; charset=utf-8
我想用php正则匹配出这段字符串:
set-cookie: session=cef14157b685078cf9c8f5d66fcefa81116a180d%7e53204bc7209fe349757896;
求补救!
------解决方案--------------------
$str = http/1.1 200 ok date: wed, 12 mar 2014 11:57:58 gmt server: apache/2.2.9 (apmserv) mod_ssl/2.2.9 openssl/0.9.8h php/5.2.6 x-powered-by: php/5.2.6 cache-control: public,max-age=10 last-modified: wed, 12 mar 2014 11:57:59 gmt set-cookie: session=cef14157b685078cf9c8f5d66fcefa81116a180d%7e53204bc7209fe349757896; expires=wed, 12-mar-2014 13:57:59 gmt; path=/; domain=.*****.com transfer-encoding: chunked content-type: text/html; charset=utf-8 ;
preg_match(/set-cookie:\s*session=[^;]+/i, $str,$match) ;
var_dump($match[0]);
------解决方案--------------------
$s=<<< txt
http/1.1 200 ok date: wed, 12 mar 2014 11:57:58 gmt server: apache/2.2.9 (apmserv) mod_ssl/2.2.9 openssl/0.9.8h php/5.2.6 x-powered-by: php/5.2.6 cache-control: public,max-age=10 last-modified: wed, 12 mar 2014 11:57:59 gmt set-cookie: session=cef14157b685078cf9c8f5d66fcefa81116a180d%7e53204bc7209fe349757896; expires=wed, 12-mar-2014 13:57:59 gmt; path=/; domain=.*****.com transfer-encoding: chunked content-type: text/html; charset=utf-8
txt;
preg_match('/(set-cookie:\s+session=.+?;)\s+expires=/s',$s,$m);
echo $m[1];