Forum Moderators: coopster

Message Too Old, No Replies

How to order matches in HTTP-header

         

wavesurf

5:15 pm on Feb 9, 2005 (gmt 0)

10+ Year Member



I Have an HTTP_ACCEPT header that I need to check for content, and I was wondering how this can be done. However, It's not as easy as matching the variables, but I also need to know which order they were found in.

Header example:
"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"

I need to check this against a set of predefined variables:
$variableTwo = "application/xhtml+xml"
$variableTwo = "application/vnd.wap.wml"
$variableThree = "text/plain"
$variableFour = "*/*"

The result needs to tell me what has been matched from the header, and in case of more than one match, which was matched first.

Anyone know how this can be done...

Thans for any help.

jatar_k

7:43 pm on Feb 9, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you can always split the headers on commas and semi colons into an array and run through them

wavesurf

8:06 pm on Feb 9, 2005 (gmt 0)

10+ Year Member



Do you know how to make an array of this when there are two separator-strings (, and ;)?

Also, sometimes there is a space after the commma, so I also have to make it work when that happens. Any ideas on how to make one array, but make sure all three conditions have been accounted for?

By the way, does anyone know what the q=0.8 and q=0.5 means in the HTTP-headers? I see those pop up in more than the HTTP_ACCEPT-string, but I can't seem to work them out?

coopster

9:12 pm on Feb 9, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$array = array_filter [php.net](explode [php.net](',', preg_replace [php.net]("/;(.*)(,¦$)/Ui", ',', $_SERVER [php.net]['HTTP_ACCEPT']))); 

There shouldn't be spaces after the comma in the HTTP_ACCEPT variable, where do you see that? Well, if there were, you would just have to loop through the array and trim() [php.net] the entries.

The qs values refer to the different source qualities as described in the HTTP/1.1 specification [rfc-editor.org]. Apache uses Content Negotiation [httpd.apache.org] to determine the variant precedence.

Don't forget that this forum breaks the pipe symbol (¦) so if you copy and paste the regular expression you will need to rekey the pipe symbol.

wavesurf

9:44 pm on Feb 9, 2005 (gmt 0)

10+ Year Member



Thanks a lot for that answer. It answered every little part of my question to a tee.

THANKS