2015-05-08 23 views
1

我有這個隨機字符串。我只需要字符串,如mcnb8h1apihg9ffav1ubtgal77內特定的文本,並Sat, 09-May-2015 11:49:58 GMT提取標頭中的cookies數據

$str = 'HTTP/1.1 302 Moved Temporarily Date: Fri, 08 May 2015 11:49:58 GMT Server: Apache/2.2.22 (Debian) X-Powered-By: PHP/5.5.23-1~dotdeb.1 Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Set-Cookie: OSTSESSID=mcnb8h1apihg9ffav1ubtgal77; expires=Sat, 09-May-2015 11:49:58 GMT; Max-Age=86400; path=/support/; domain=myweb.com; secure Location: index.php Vary: Accept-Encoding Transfer-Encoding: chunked Content-Type: text/html; charset=utf-8 HTTP/1.1 200 OK Date: Fri, 08 May 2015 11:49:58 GMT Server: Apache/2.2.22 (Debian) X-Powered-By: PHP/5.5.23-1~dotdeb.1 Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Set-Cookie: OSTSESSID=sugrlug6sj8m27lrkv5id9v473; expires=Sat, 09-May-2015 11:49:58 GMT; Max-Age=86400; path=/support/; domain=myweb.com; secure Vary: Accept-Encoding Transfer-Encoding: chunked Content-Type: text/html; charset=UTF-8'; 

此字符串是完全隨機的。我試圖解決它:

$pattern = "#OSTSESSID=(.*); expires=(.*)\n#"; 
preg_match_all($pattern, $str, $matches); 

print_r($matches); 

但沒有奏效。請幫我解決這個問題。

+0

有什麼理由'$ _COOKIE [ 'OSTSESSID']'是行不通的? – Machavity

回答

1

您應該可能read up on Regex和您可以使用的各種運營商。

並使用https://regex101.com/作爲排除故障的好地方。

您還沒有提供關於您的數據的很多信息,是OSTSESSID每次都是一樣的長度,總是小寫字母數字?

如果我們假設的話,那麼這裏有一個提示/半答案:

$pattern = "/OSTSESSID=([a-z0-9]{26});/"; 

希望這將有助於你的休息。

1

它不工作的一大原因是,你得到的貪婪匹配。*的

它試圖匹配儘可能多的字符可能。在明星之後投擲一個問號使其不合格:

OSTSESSID =(。?);期滿=; (I也終止了到期位用分號 ,只是相匹配的片段(。):

OSTSESSID = mcnb8h1apihg9ffav1ubtgal77;期滿=星期六,09可能 - 2015 11點49分58秒格林尼治標準時間; 和 OSTSESSID = sugrlug6sj8m27lrkv5id9v473;到期=週六,09月2015年11時49分58秒格林尼治標準時間;