GreenVine
GreenVine

Reputation: 15

Regex Matching Custom HTTP Header

I have a full text of HTTP header like this:

GET /index.php HTTP/1.1
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
X-ACCESS: 123456

So I want to create a regex which checks if X-ACCESS is exists and equals to 123456, how should I write it?

EDIT: I do not need to control the value of 123456, that is as long as X-ACCESS: 123456 is presented, regex will be true.

Thanks in advance :)

Upvotes: 0

Views: 504

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174816

You could use preg_match function.

$str = <<<EOT
GET /index.php HTTP/1.1
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
X-ACCESS: 123456
EOT;
if(preg_match('~^X-ACCESS:\h*123456$~m', $str)) {

        echo "Yep, they are equal";
}

\h* will match zero or more horizontal white-space character, you could use \s* also.

Upvotes: 0

Forien
Forien

Reputation: 2763

Why regex? This will suffice:

if (stripos ($http_header_text, 'X-ACCESS: 123456') !== false) {
    echo 'Custom header is here and is good';
}

Upvotes: 2

Related Questions