regex - Regular Expression to match multiple patterns in Php

220

I try to extract the Cookies from the following String:

< Server: Apache
< Set-Cookie: fe_typo_user=b4f33487c434684655dccf65a0499010; path=/
< Set-Cookie: PHPSESSID=d8pgb31olkc9c1jc5tc5qn23a1; path=/
< Expires: Thu, 19 Nov 1981 08:52:00 GMT

Therefore I tried

/.*Set-Cookie: (.*?); /s

Shouldn't that get me all occurrences of strings between "Set-Cookie: " and ";"?

I received:

array(2) {
   [0]=> string(137) \"< Server: Apache
         < Set-Cookie: fe_typo_user=b4f33487c434684655dccf65a0499010; path=/
         < Set-Cookie: PHPSESSID=d8pgb31olkc9c1jc5tc5qn23a1; \"
   [1]=> string(36) \"PHPSESSID=d8pgb31olkc9c1jc5tc5qn23a1\"
}

Thanks for explanation.

7

Answer

Solution:

preg_match_all('/Set-Cookie: (.*?);/is', $data, $result);

Gives:

Array
(
    [0] => Array
    (
        [0] => Set-Cookie: fe_typo_user=b4f33487c434684655dccf65a0499010;
        [1] => Set-Cookie: PHPSESSID=d8pgb31olkc9c1jc5tc5qn23a1;
    )

    [1] => Array
    (
        [0] => fe_typo_user=b4f33487c434684655dccf65a0499010
        [1] => PHPSESSID=d8pgb31olkc9c1jc5tc5qn23a1
    )

)
450

Answer

Solution:

Just remove the first.* and use preg_match_all:

preg_match_all('/Set-Cookie: (.*?); /s', $str, $m);
593

Answer

Solution:

Try this one

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

People are also looking for solutions to the problem: php - htaccess https rewrite issue when trying to login to cmsms

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.