0

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

2 Answers 2

2

Why regex? This will suffice:

if (stripos ($http_header_text, 'X-ACCESS: 123456') !== false) {
    echo 'Custom header is here and is good';
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for your answer :) Well I only want regex because it's not in the programming environment, web server instead.
@GreenVine you used php tag, so I assume you can use php. If yes, then can use the code above.
does it checks this and equals to 123456 condition?
@AvinashRaj ofc it does. stripos() returns true if entire X-ACCESS: 123456 is found. if there is X-ACCESS: 121212 it will return false.
i think op wants to check the value of X-ACCESS: with the value stored inside another variable.
|
0

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.

5 Comments

why not check for '123456' within regex already? Or why don't just check stripos or stristr?
Can you provide a regex checks 123456 within the regex like @Forien said? That is as long as X-ACCESS: 123456 is presented regex is true
@GreenVine the simpliest regex for that is X-ACCESS: 123456.
@Forien You are absolutely right X-ACCESS: 123456 is the simplest regex, just made it complicated :lol
but this X-ACCESS: 123456 regex will match also foobarX-ACCESS: 12345673647634 :lol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.