Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5954507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:58:52+00:00 2026-05-22T17:58:52+00:00

The variable http_accept output is a mess, i can’t work with it, need to

  • 0

The variable http_accept output is a mess, i can’t work with it, need to transform into an array like this:

Array
(
    [en-ca] => 1
    [pt-pt] => 0.4
    [de] => 0.2
);

I hope you guys can help me.

EDIT:

Here is a code that will detect the preferred language of the client, this is a quick way to guess the user’s language:

I’ve done this thinking of all Spanish and Portuguese visitors, there are some substr in the midle, because pt-BR or pt-PT will use the same language files, the same for es-ES and es-CO for example.

<?php
// detectar idioma
$linguagens = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$parcelas = explode(',', $linguagens);
$linguas_aceites = array();

foreach ($parcelas as $lingua) {
        $contagem = preg_match('/([-a-zA-Z]+)\s*;\s*q=([0-9\.]+)/', $lingua, $matches);
        if ($contagem === 0)
           $linguas_aceites[substr($lingua, 0, 2)] = 1;
        else
           $linguas_aceites[substr($matches[1], 0, 2)] = $matches[2];
}

foreach ($linguas_aceites as $key => $val) { 
      if(substr($key, 0, 2) == "pt"){
        echo "Portugues e preferido aqui";}
}   

?>

This wouldn’t be possible without Aif’s help (Thanks a lot for the original code!):

<?php

    $al = 'en-US,en;q=0.8,pt-PT;q=0.6';
    $values = explode(',', $al);

    $accept_language = array();

    foreach ($values AS $lang) {
            $cnt = preg_match('/([-a-zA-Z]+)\s*;\s*q=([0-9\.]+)/', $lang, $matches);
            if ($cnt === 0)
               $accept_language[$lang] = 1;
            else
               $accept_language[$matches[1]] = $matches[2];
    }

    print_r($accept_language);
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:58:52+00:00Added an answer on May 22, 2026 at 5:58 pm

    Look likes this is your answer 🙂

    Attempt to explain one algorithm: (tested only with your sample)

    // Get the accept-language header
    $al = 'en-US,en;q=0.8,pt-PT;q=0.6'
    

    Notice that each lang is separated by a comma , and for some language, you have an order of preference (the float stuff).

    First, get the values inside the commas:

    $values = explode(',', $al);
    

    Then for each value, if there is a ;, retrieve the value for the left hand key and store it in an array; otherwize, store the lang as a key, and take 1 as a value.
    I use the fact that preg_match returns the number of match (then 0 if none, in our case, the first one).

    foreach ($values AS $lang) {
           $cnt = preg_match('/([-a-zA-Z]+)\s*;\s*q=([0-9\.]+)/', $lang, $matches);
           if ($cnt === 0)
               $accept_language[$lang] = 1;
           else
               $accept_language[$matches[1]] = $matches[2];
    }
    

    The whole script:

    <?php
    
    $al = 'en-US,en;q=0.8,pt-PT;q=0.6';
    $values = explode(',', $al);
    
    $accept_language = array();
    
    foreach ($values AS $lang) {
            $cnt = preg_match('/([-a-zA-Z]+)\s*;\s*q=([0-9\.]+)/', $lang, $matches);
            if ($cnt === 0)
               $accept_language[$lang] = 1;
            else
               $accept_language[$matches[1]] = $matches[2];
    }
    
    print_r($accept_language);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to save the returned data from HTTP request into a variable. The
I wish to be able to generate URL variables like this: http://example.com/195yq http://example.com/195yp http://example.com/195yg
I have a simple server that looks something like this: void *run_thread(void *arg) {
After an incident at work where I misused String.IsNullOrEmpty with a Session variable, a
I am trying to get Session Value and store in the variable.. how can
I'm working on application which has workflow like this: 1.parsing home page (using HttpURLConnection,
Thanks in advance for any assistance anyone can provide with this torturous issue. I
I have asked a question on this before, but again ran into problems and
$_GET variable shows empty on the URL www.abc.com/ec/basket.php?product_id=1 . This is very surprising as
I have an array of results from a query in a variable called $result

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.