从字符串中提取 PHPSESSID 值的正则表达式模式
Regex pattern to extract PHPSESSID value from string
我在使用 preg_match()
时遇到一些问题。
我使用了这段代码(过去它运行得很好):
preg_match("/PHPSESSID=(.*?)(?:;|\r\n)/", $code, $phpsessid);
但现在它不再起作用了。 (returns 一个空数组)。
我的主题:HTTP/1.1 302 Moved Temporarily Server: nginx/1.8.0 Date: Wed, 24 May 2017 08:58:57 GMT Content-Type: text/html Transfer-Encoding: chunked Connection: keep-alive X-Powered-By: PHP/5.3.10-1ubuntu3.18 Set-Cookie: PHPSESSID=jrq8446q91fv6eme2ois3lpl07; expires=Thu, 24-May-2018 08:58:57 GMT; path=/; Expires: Thu, 19 Nov 1981 08:52:00 GMT Pragma: no-cache Cache-Control: no-store, no-cache, must-revalidate Location: index.php
*
我需要获取 PHPSESSID 值:jrq8446q91fv6eme2ois3lpl07
感谢您的回答。
那http://www.php.net/session_id呢?
$sessionid = session_id();
/拉斯
尽量不要将 ?与 (.*) 所以:
preg_match("/PHPSESSID=(.*)?(:;|\r\n)?/", $code, $phpsessid);
给定 OP 的输入字符串...
OP 的模式有效Pattern Demo(131 步)
目前接受的答案是不正确——这肯定会让未来的读者感到困惑。 Pattern Demo
但我们要确保您使用的是最有效、最简短、最好的模式...
/PHPSESSID=\K[a-z\d]*/ #no capture group, 23 steps (accurate for sample input)
/PHPSESSID=\K[^;]*/ #no capture group, 23 steps (accurate for sample input)
/PHPSESSID=\K\w*/ #no capture group, 23 steps (not inaccurate, includes underscores)
如果您希望看到 \r
或 \n
作为 PHPSESSID 值的可能分隔符,则可以将这些字符添加到 "negated character class",如下所示:[^;\r\n]
(它仍然会 运行 23 步)Pattern Demo
输入:
$subject='HTTP/1.1 302 Moved Temporarily Server: nginx/1.8.0 Date: Wed, 24 May 2017 08:58:57 GMT Content-Type: text/html Transfer-Encoding: chunked Connection: keep-alive X-Powered-By: PHP/5.3.10-1ubuntu3.18 Set-Cookie: PHPSESSID=jrq8446q91fv6eme2ois3lpl07; expires=Thu, 24-May-2018 08:58:57 GMT; path=/; Expires: Thu, 19 Nov 1981 08:52:00 GMT Pragma: no-cache Cache-Control: no-store, no-cache, must-revalidate Location: index.php
*';
单行法(PHP Demo):
echo preg_match('/PHPSESSID=\K[^;\r\n]*/',$subject,$out)?$out[0]:'';
输出:
jrq8446q91fv6eme2ois3lpl07
请注意,通过使用 \K
,无需使用捕获组,这会将输出数组大小减少 50%。我希望这些最佳实践对未来的读者有所启发。
我在使用 preg_match()
时遇到一些问题。
我使用了这段代码(过去它运行得很好):
preg_match("/PHPSESSID=(.*?)(?:;|\r\n)/", $code, $phpsessid);
但现在它不再起作用了。 (returns 一个空数组)。
我的主题:HTTP/1.1 302 Moved Temporarily Server: nginx/1.8.0 Date: Wed, 24 May 2017 08:58:57 GMT Content-Type: text/html Transfer-Encoding: chunked Connection: keep-alive X-Powered-By: PHP/5.3.10-1ubuntu3.18 Set-Cookie: PHPSESSID=jrq8446q91fv6eme2ois3lpl07; expires=Thu, 24-May-2018 08:58:57 GMT; path=/; Expires: Thu, 19 Nov 1981 08:52:00 GMT Pragma: no-cache Cache-Control: no-store, no-cache, must-revalidate Location: index.php
*
我需要获取 PHPSESSID 值:jrq8446q91fv6eme2ois3lpl07
感谢您的回答。
那http://www.php.net/session_id呢?
$sessionid = session_id();
/拉斯
尽量不要将 ?与 (.*) 所以:
preg_match("/PHPSESSID=(.*)?(:;|\r\n)?/", $code, $phpsessid);
给定 OP 的输入字符串...
OP 的模式有效Pattern Demo(131 步)
目前接受的答案是不正确——这肯定会让未来的读者感到困惑。 Pattern Demo
但我们要确保您使用的是最有效、最简短、最好的模式...
/PHPSESSID=\K[a-z\d]*/ #no capture group, 23 steps (accurate for sample input)
/PHPSESSID=\K[^;]*/ #no capture group, 23 steps (accurate for sample input)
/PHPSESSID=\K\w*/ #no capture group, 23 steps (not inaccurate, includes underscores)
如果您希望看到 \r
或 \n
作为 PHPSESSID 值的可能分隔符,则可以将这些字符添加到 "negated character class",如下所示:[^;\r\n]
(它仍然会 运行 23 步)Pattern Demo
输入:
$subject='HTTP/1.1 302 Moved Temporarily Server: nginx/1.8.0 Date: Wed, 24 May 2017 08:58:57 GMT Content-Type: text/html Transfer-Encoding: chunked Connection: keep-alive X-Powered-By: PHP/5.3.10-1ubuntu3.18 Set-Cookie: PHPSESSID=jrq8446q91fv6eme2ois3lpl07; expires=Thu, 24-May-2018 08:58:57 GMT; path=/; Expires: Thu, 19 Nov 1981 08:52:00 GMT Pragma: no-cache Cache-Control: no-store, no-cache, must-revalidate Location: index.php
*';
单行法(PHP Demo):
echo preg_match('/PHPSESSID=\K[^;\r\n]*/',$subject,$out)?$out[0]:'';
输出:
jrq8446q91fv6eme2ois3lpl07
请注意,通过使用 \K
,无需使用捕获组,这会将输出数组大小减少 50%。我希望这些最佳实践对未来的读者有所启发。