正则表达式模式从字符串中提取PHPSESSID值

时间:2022-09-13 12:20:12

I am having some issues with preg_match(). I used this code (and it used to worked pretty well in the past):

我在使用preg_match()时遇到了一些问题。我使用过这段代码(以前它曾经很好用):

preg_match("/PHPSESSID=(.*?)(?:;|\r\n)/", $code, $phpsessid);

but now it's not working anymore. (returns an empty array).

但现在它不再起作用了。 (返回一个空数组)。

My 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 *

我的主题:HTTP / 1.1 302暂时移动服务器:nginx / 1。8。0日期:2017年5月24日星期三08:58:57 GMT内容类型:text / html传输编码:chunked连接:keep-alive X-Powered-作者:PHP / 5.3.10-1ubuntu3.18 Set-Cookie:PHPSESSID = jrq8446q91fv6eme2ois3lpl07; expires =星期四,24-May-2018 08:58:57 GMT;路径= /; Expires:Thu,1981年11月19日08:52:00 GMT Pragma:no-cache Cache-Control:no-store,no-cache,must-revalidate位置:index.php *

I need to get the PHPSESSID value: jrq8446q91fv6eme2ois3lpl07

我需要获取PHPSESSID值:jrq8446q91fv6eme2ois3lpl07

Thanks for your answers.

谢谢你的回答。

3 个解决方案

#1


0  

Try not grouping the ? with the (.*) so:

尝试不分组?与(。*)所以:

preg_match("/PHPSESSID=(.*)?(:;|\r\n)?/", $code, $phpsessid);

#2


0  

What about http://www.php.net/session_id ?

那么http://www.php.net/session_id呢?

$sessionid = session_id();

$ sessionid = session_id();

/Lasse

#3


0  

Given the OP's input string...

鉴于OP的输入字符串......

The OP's pattern works Pattern Demo (131 steps)

OP的模式工作模式演示(131步)

The currently accepted answer is incorrect -- this will surely confuse future readers. Pattern Demo

目前接受的答案是错误的 - 这肯定会混淆未来的读者。模式演示

But let's make sure you are using the most efficient, most brief, best possible pattern...

但是,让我们确保您使用最有效,最简短,最好的模式......

/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)

If you expect to see \r or \n as a possible delimiter for the PHPSESSID value, then you can add those characters to the "negated character class" like this: [^;\r\n] (it will still run in 23 steps) Pattern Demo

如果您希望将\ r或\ n视为PHPSESSID值的可能分隔符,则可以将这些字符添加到“否定字符类”中,如下所示:[^; \ r \ n](它仍将在23中运行步骤)模式演示

Input:

$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
*';

One-liner Method (PHP Demo):

单线方法(PHP演示):

echo preg_match('/PHPSESSID=\K[^;\r\n]*/',$subject,$out)?$out[0]:'';

Output:

jrq8446q91fv6eme2ois3lpl07

Notice that by using \K there is no need to use a capture group, this reduces output array size by 50%. I hope these best practices educate future readers.

请注意,使用\ K不需要使用捕获组,这会将输出数组大小减少50%。我希望这些最佳实践能够教育未来的读者。

#1


0  

Try not grouping the ? with the (.*) so:

尝试不分组?与(。*)所以:

preg_match("/PHPSESSID=(.*)?(:;|\r\n)?/", $code, $phpsessid);

#2


0  

What about http://www.php.net/session_id ?

那么http://www.php.net/session_id呢?

$sessionid = session_id();

$ sessionid = session_id();

/Lasse

#3


0  

Given the OP's input string...

鉴于OP的输入字符串......

The OP's pattern works Pattern Demo (131 steps)

OP的模式工作模式演示(131步)

The currently accepted answer is incorrect -- this will surely confuse future readers. Pattern Demo

目前接受的答案是错误的 - 这肯定会混淆未来的读者。模式演示

But let's make sure you are using the most efficient, most brief, best possible pattern...

但是,让我们确保您使用最有效,最简短,最好的模式......

/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)

If you expect to see \r or \n as a possible delimiter for the PHPSESSID value, then you can add those characters to the "negated character class" like this: [^;\r\n] (it will still run in 23 steps) Pattern Demo

如果您希望将\ r或\ n视为PHPSESSID值的可能分隔符,则可以将这些字符添加到“否定字符类”中,如下所示:[^; \ r \ n](它仍将在23中运行步骤)模式演示

Input:

$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
*';

One-liner Method (PHP Demo):

单线方法(PHP演示):

echo preg_match('/PHPSESSID=\K[^;\r\n]*/',$subject,$out)?$out[0]:'';

Output:

jrq8446q91fv6eme2ois3lpl07

Notice that by using \K there is no need to use a capture group, this reduces output array size by 50%. I hope these best practices educate future readers.

请注意,使用\ K不需要使用捕获组,这会将输出数组大小减少50%。我希望这些最佳实践能够教育未来的读者。