我有这个随机字符串。我只需要字符串中的特定文本,例如 mcnb8h1apihg9ffav1ubtgal77
和 Sat, 09-May-2015 11:49:58 GMT
$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);
但行不通。请帮助我解决这个问题。
您可能应该在Regex和可以使用的各种操作员上阅读。
并使用https://regex101.com/作为故障排除的好地方。
您没有提供有关数据的太多信息, ostsessid 每次都相同的长度,总是下案字母数字?
如果我们这样做,那么这是您的答案的提示/一半:
$pattern = "/OSTSESSID=([a-z0-9]{26});/";
希望这将为您提供其余的帮助。
它不起作用的重要原因是您在。*'s
它试图匹配尽可能多的字符。在恒星之后,向其扔一个问号:
ostSessId =(。?);到期=(。 ?;)(我还用半柱子终止了钻头只与摘要相匹配:
ostsessid = mcnb8h1apihg9ffav1ubtgal77;到期=星期六,2015年5月11日11:49:58 GMT;和ostSessID = sugrlug6SJ8M27LRKV5ID9V473;Expires = SAT,09-May-2015 11:49:58 GMT;