Page 1 of 1

RegEx Help - Not parsing like it should

Posted: Wed Jan 17, 2007 10:38 pm
by aliasxneo
Here is my code:

Code: Select all

private function parse_cookies()
    {
        $lines = split($this->newline, $this->header);

        foreach ($lines as $line)
        {
            if (preg_match("/Set-Cookie\: (.*)=(.*)\;/", $line, $matches))
            {
                $this->cookies[$matches[1]] = $matches[2];
            }
        }
    }
Basically I am parsing cookies from an HTTP header. It works pretty good except that the RegEx is parsing more than it should. Here is the output when printing the cookies array:
neodebug=deleted; expires=Wed, 18-Jan-06 04:33:13 GMT; path = /
nupi=0; expires=Thu, 18-Jan-07 02:53:14 GMT; path = /
nupid=0; expires=Thu, 18-Jan-07 02:53:14 GMT; path = /
npid=0; expires=Thu, 18-Jan-07 02:53:14 GMT; path = /
np_uniq=pending; expires=Fri, 18-Jan-08 04:33:14 GMT; path = /
xt6Yr4e33D=59182809479467180131245; expires=Fri, 18-Jan-08 04:33:14 GMT; path = /
Basically what it's doing is skipping the ";" and grabbing the rest of the line. Take the first line for example, it's "neodebug=deleted; expires", as you can see there is a ";" there but in my RegEx I wanted it to stop at the ";" and only grab the things before it but as seen here it's just completely skipping it. Is there a reason for this? Thanks.

Cheers,
- Josh

Posted: Wed Jan 17, 2007 11:24 pm
by volka
preg_match runs in greedy mode by default. (.*) tries to match as much characters as possible.
try

Code: Select all

/Set-Cookie: ([^=]+)=([^;]+);/
or

Code: Select all

/Set-Cookie: (.+)=(.+);/U

Posted: Thu Jan 18, 2007 6:13 am
by Kieran Huggins
um.... just checking but you could use $_COOKIE if you're dealing with current http headers.

also, you can modify the greediness with a trailing ? like so:

Code: Select all

/Set-Cookie: (.+?)=(.+?);/

Posted: Thu Jan 18, 2007 8:21 am
by Ollie Saunders
Kireran's code should work. As you can see you don't need to escape the semi-colon or colon.