apreg match类型的游戏有哪些?

authentication - PHP Curl And Cookies - Stack Overflow
Stack Overflow for Teams
A private, secure home for your team's questions and answers.
to customize your list.
This site uses cookies to deliver our services and to show you relevant ads and job listings.
By using our site, you acknowledge that you have read and understand our , , and our .
Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.
I have some problem with PHP Curl and Cookies Authentication.
I have a file Connector.php which authenticates users on another server and returns cookie with current user.
The Problem is that I want to authenticate
thousands of users with curl but it authenticates and saves COOKIES only for one user at a time.
the code for connector.php is this:
if(!count($_REQUEST)) {
die("No Access!");
//Core Url For Services
define ('ServiceCore', 'http://example.com/core/');
//Which Internal Service Should Be Called
$path = $_GET['service'];
//Service To Be Queried
$url = ServiceCore.$
//Open the Curl session
$session = curl_init($url);
// If it's a GET, put the GET data in the body
if ($_GET['service']) {
//Iterate Over GET Vars
$postvars = '';
foreach($_GET as $key=&$val) {
if($key!='service') {
$postvars.="$key=$val&";
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $postvars);
//Create And Save Cookies
$tmpfname = dirname(__FILE__).'/cookie.txt';
curl_setopt($session, CURLOPT_COOKIEJAR, $tmpfname);
curl_setopt($session, CURLOPT_COOKIEFILE, $tmpfname);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);
// EXECUTE
$json = curl_exec($session);
curl_close($session);
Here is the process of authentication:
User enters username and password: Connector.php?service=logon&user_name=user32&user_pass=123
Connector.php?service=logosessionInfo this returns info about user based on cookies saved earlier with logon service.
The problem is that this code saves Cookie in one file for one user and cant handle multiple user authentication.
11.7k104568
You can specify the cookie file with a curl opt. You could use a unique file for each user.
curl_setopt( $curl_handle, CURLOPT_COOKIESESSION, true );
curl_setopt( $curl_handle, CURLOPT_COOKIEJAR, uniquefilename );
curl_setopt( $curl_handle, CURLOPT_COOKIEFILE, uniquefilename );
The best way to handle it would be to stick your request logic into a curl function and just pass the unique file name in as a parameter.
function fetch( $url, $z=null ) {
curl_init();
$useragent = isset($z['useragent']) ? $z['useragent'] : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/ Firefox/10.0.2';
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_POST, isset($z['post']) );
if( isset($z['post']) )
curl_setopt( $ch, CURLOPT_POSTFIELDS, $z['post'] );
if( isset($z['refer']) )
curl_setopt( $ch, CURLOPT_REFERER, $z['refer'] );
curl_setopt( $ch, CURLOPT_USERAGENT, $useragent );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, ( isset($z['timeout']) ? $z['timeout'] : 5 ) );
curl_setopt( $ch, CURLOPT_COOKIEJAR,
$z['cookiefile'] );
curl_setopt( $ch, CURLOPT_COOKIEFILE, $z['cookiefile'] );
$result = curl_exec( $ch );
curl_close( $ch );
I use this for quick grabs. It takes the url and an array of options.
In working with a similar problem I created the following function after combining a lot of resources I ran into on the web, and adding my own cookie handling. Hopefully this is useful to someone else.
function get_web_page( $url, $cookiesIn = '' ){
$options = array(
CURLOPT_RETURNTRANSFER =& true,
// return web page
CURLOPT_HEADER
//return headers in addition to content
CURLOPT_FOLLOWLOCATION =& true,
// follow redirects
CURLOPT_ENCODING
// handle all encodings
CURLOPT_AUTOREFERER
// set referer on redirect
CURLOPT_CONNECTTIMEOUT =& 120,
// timeout on connect
CURLOPT_TIMEOUT
// timeout on response
CURLOPT_MAXREDIRS
// stop after 10 redirects
CURLINFO_HEADER_OUT
CURLOPT_SSL_VERIFYPEER =& true,
// Validate SSL Certificates
CURLOPT_HTTP_VERSION
=& CURL_HTTP_VERSION_1_1,
CURLOPT_COOKIE
=& $cookiesIn
= curl_init( $url );
curl_setopt_array( $ch, $options );
$rough_content = curl_exec( $ch );
= curl_errno( $ch );
= curl_error( $ch );
= curl_getinfo( $ch );
curl_close( $ch );
$header_content = substr($rough_content, 0, $header['header_size']);
$body_content = trim(str_replace($header_content, '', $rough_content));
$pattern = "#Set-Cookie:\\s+(?&cookie&[^=]+=[^;]+)#m";
preg_match_all($pattern, $header_content, $matches);
$cookiesOut = implode("; ", $matches['cookie']);
$header['errno']
$header['errmsg']
$header['headers']
= $header_
$header['content'] = $body_
$header['cookies'] = $cookiesO
2,67133860
First create temporary cookie using tempnam() function:
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
Than execute curl init witch cookie saves as a temporary file:
$ch = curl_init ("http://uri.com/");
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
Or visit cookie data where is store of temporary data:
$ch = curl_init ("http://somedomain.com/cookiepage.php");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
This will loads page of cookie initialization:
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
2,90631643
Here you can find some useful info about cURL & cookies
You can also use this well done method
like a function:
function CURL($url, $data = null, $method = 'GET', $cookie = null, $options = null, $retries = 3)
if ((extension_loaded('curl') === true) && (is_resource($curl = curl_init()) === true))
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
if (preg_match('~^(?:DELETE|GET|HEAD|OPTIONS|POST|PUT)$~i', $method) & 0)
if (preg_match('~^(?:HEAD|OPTIONS)$~i', $method) & 0)
curl_setopt_array($curl, array(CURLOPT_HEADER =& true, CURLOPT_NOBODY =& true));
else if (preg_match('~^(?:POST|PUT)$~i', $method) & 0)
if (is_array($data) === true)
foreach (preg_grep('~^@~', $data) as $key =& $value)
$data[$key] = sprintf('@%s', rtrim(str_replace('\\', '/', realpath(ltrim($value, '@'))), '/') . (is_dir(ltrim($value, '@')) ? '/' : ''));
if (count($data) != count($data, COUNT_RECURSIVE))
$data = http_build_query($data, '', '&');
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, strtoupper($method));
if (isset($cookie) === true)
curl_setopt_array($curl, array_fill_keys(array(CURLOPT_COOKIEJAR, CURLOPT_COOKIEFILE), strval($cookie)));
if ((intval(ini_get('safe_mode')) == 0) && (ini_set('open_basedir', null) !== false))
curl_setopt_array($curl, array(CURLOPT_MAXREDIRS =& 5, CURLOPT_FOLLOWLOCATION =& true));
if (is_array($options) === true)
curl_setopt_array($curl, $options);
for ($i = 1; $i &= $ ++$i)
$result = curl_exec($curl);
if (($i == $retries) || ($result !== false))
usleep(pow(2, $i - 2) * 1000000);
curl_close($curl);
And pass this as $cookie parameter:
$cookie_jar = tempnam('/tmp','cookie');
You can define different cookies for every user with CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR. Make different file for every user so each one would have it's own cookie-based session on remote server.
8,301124576
Solutions which are described above, even with unique CookieFile names, can cause a lot of problems on scale.
We had to serve a lot of authentications with this solution and our server went down because of high file read write actions.
The solution for this was to use Apache Reverse Proxy and omit CURL requests at all.
Details how to use Proxy on Apache can be found here:
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Post as a guest
Post as a guest
By clicking &Post Your Answer&, you acknowledge that you have read our updated ,
and , and that your continued use of the website is subject to these policies.
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabledregex - php only allow letters, numbers, spaces and specific symbols using pregmatch - Stack Overflow
Stack Overflow for Teams
A private, secure home for your team's questions and answers.
to customize your list.
This site uses cookies to deliver our services and to show you relevant ads and job listings.
By using our site, you acknowledge that you have read and understand our , , and our .
Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.
on my php i use preg_match to validate input texts.
if(preg_match('/^[a-zA-Z0-9]+$/', $firstname)) {
But this only allows alphanumeric and does not allow spaces. I want to allow spaces, alpha and numeric. and period(.) and dash(-)
Please help me out here? thanks in advance.
134k33190271
preg_match('/^[a-z0-9 .\-]+$/i', $firstname)
75.7k20126187
If you not only want to allow ASCII, then use :
preg_match('/^[\p{L}\p{N} .-]+$/', $firstname)
\p{L} is any letter in any language, matches also Chinese, Hebrew, Arabic, ... characters.
\p{N} any kind of numeric character (means also e.g. roman numerals)
if you want to limit to digits, then use \p{Nd}
60.1k1176104
The only difficult bit here is the dash.
For spaces and dots, you can simply add them to your character class, like so:
'/^[a-zA-Z0-9 .]+$/'
The dash is slightly harder because hyphens have special meaning in a character class like this (as you already know, they're used for ranges of characters like a-z). In order to specify a hyphen in a character class, it must be the first character in the class (ie it must not be between two characters, otherwise it is treated as a character range marker).
So your expression would be:
'/^[-a-zA-Z0-9 .]+$/'
Hope that helps.
134k33190271
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Post as a guest
Post as a guest
By clicking &Post Your Answer&, you acknowledge that you have read our updated ,
and , and that your continued use of the website is subject to these policies.
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled求助:PHP preg_match_all匹配的问题_百度知道
求助:PHP preg_match_all匹配的问题
【A】[a][b][c]【B】[a][b][c]【C】[a][b][c]如图所示,我既要匹配出ABC,又要匹配出其下面的abc(现在只能取到ABC和下面的a),不知用preg_match_all能做到吗,怎么实现?
我有更好的答案
【A】[a][b][c]
【B】[a][b][c]
【C】[a][b][c]&;
if(preg_match_all('/[a-zA-Z]/',$str,$arr)){
echo '&pre&';print_r($arr);
echo '没有找到匹配的';
//不知道你要的是哪一种
if(preg_match_all('/【([A-Z])】(\[([a-z])\]\[([a-z])\]\[([a-z])\])+/',$str,$arr)){
echo '&pre&';print_r($arr);
echo '没有找到匹配的';
呃,是我没说清楚,我的目的是这样的,匹配出店铺名称假设为A\B\C……,店铺A下又有商品名称假设为a\b\c(数量不定),同样店铺B下有商品a\b\c……怎样获得每个店铺的名称和对应的所有商品名称呢?谢谢~~!假设代码为&div&&p&&ul&店铺A&li&商品a&/li&&/ul&&/p&&p&&ul&店铺B&li&商品a&/li&&li&商品b&/li&&/ul&&/p&&p&&ul&店铺C&li&商品a&/li&&li&商品b&/li&&li&商品c&/li&&/ul&&/p&&/div&
为您推荐:
其他类似问题
match的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。Detect Browser Language in PHP - Stack Overflow
Stack Overflow for Teams
A private, secure home for your team's questions and answers.
to customize your list.
This site uses cookies to deliver our services and to show you relevant ads and job listings.
By using our site, you acknowledge that you have read and understand our , , and our .
Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.
I use the following PHP script as index for my website.
This script should include a specific page depending on the browser's language (automatically detected).
This script does not work well with all browsers, so it always includes index_en.php for any detected language (the cause of the problem is most probably an issue with some Accept-Language header not being considered).
Could you please suggest me a more robust solution?
// Open session var
session_start();
// views: 1 = &1 = second visit
// Detect language from user agent browser
function lixlpixel_get_env_var($Var)
if(empty($GLOBALS[$Var]))
$GLOBALS[$Var]=(!empty($GLOBALS['_SERVER'][$Var]))?
$GLOBALS['_SERVER'][$Var] : (!empty($GLOBALS['HTTP_SERVER_VARS'][$Var])) ? $GLOBALS['HTTP_SERVER_VARS'][$Var]:'';
function lixlpixel_detect_lang()
// Detect HTTP_ACCEPT_LANGUAGE & HTTP_USER_AGENT.
lixlpixel_get_env_var('HTTP_ACCEPT_LANGUAGE');
lixlpixel_get_env_var('HTTP_USER_AGENT');
$_AL=strtolower($GLOBALS['HTTP_ACCEPT_LANGUAGE']);
$_UA=strtolower($GLOBALS['HTTP_USER_AGENT']);
// Try to detect Primary language if several languages are accepted.
foreach($GLOBALS['_LANG'] as $K)
if(strpos($_AL, $K)===0)
return $K;
// Try to detect any language if not yet detected.
foreach($GLOBALS['_LANG'] as $K)
if(strpos($_AL, $K)!==false)
return $K;
foreach($GLOBALS['_LANG'] as $K)
//if(preg_match("/[[( ]{$K}[;,_-)]/",$_UA)) // matching other letters (create an error for seo spyder)
return $K;
// Return default language if language is not yet detected.
return $GLOBALS['_DLANG'];
// Define default language.
$GLOBALS['_DLANG']='en';
// Define all available languages.
// WARNING: uncomment all available languages
$GLOBALS['_LANG'] = array(
'af', // afrikaans.
'ar', // arabic.
'bg', // bulgarian.
'ca', // catalan.
'cs', // czech.
'da', // danish.
'de', // german.
'el', // greek.
'en', // english.
'es', // spanish.
'et', // estonian.
'fi', // finnish.
'fr', // french.
'gl', // galician.
'he', // hebrew.
'hi', // hindi.
'hr', // croatian.
'hu', // hungarian.
'id', // indonesian.
'it', // italian.
'ja', // japanese.
'ko', // korean.
'ka', // georgian.
'lt', // lithuanian.
'lv', // latvian.
'ms', // malay.
'nl', // dutch.
'no', // norwegian.
'pl', // polish.
'pt', // portuguese.
'ro', // romanian.
'ru', // russian.
'sk', // slovak.
'sl', // slovenian.
'sq', // albanian.
'sr', // serbian.
'sv', // swedish.
'th', // thai.
'tr', // turkish.
'uk', // ukrainian.
'zh' // chinese.
// Redirect to the correct location.
// Example Implementation aff var lang to name file
echo 'The Language detected is: '.lixlpixel_detect_lang(); // For Demonstration
echo "&br /&";
$lang_var = lixlpixel_detect_lang(); //insert lang var system in a new var for conditional statement
echo "&br /&";
echo $lang_ // print var for trace
echo "&br /&";
// Insert the right page iacoording with the language in the browser
switch ($lang_var){
case "fr":
//echo "PAGE DE";
include("index_fr.php");//include check session DE
case "it":
//echo "PAGE IT";
include("index_it.php");
case "en":
//echo "PAGE EN";
include("index_en.php");
//echo "PAGE EN - Setting Default";
include("index_en.php");//include EN in all other cases of different lang detection
31.5k102303513
why dont you keep it simple and clean
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
case "fr":
//echo "PAGE FR";
include("index_fr.php");//include check session FR
case "it":
//echo "PAGE IT";
include("index_it.php");
case "en":
//echo "PAGE EN";
include("index_en.php");
//echo "PAGE EN - Setting Default";
include("index_en.php");//include EN in all other cases of different lang detection
18.1k65893
10.8k32734
is a list of weighted values (see q parameter). That means just looking at the first language does not mean it’s als in fact, a q value of 0 means not acceptable at all.
So instead of just looking at the first language, parse the list of accepted languages and available languages and find the best match:
// parse list of comma separated language tags and sort it by the quality value
function parseLanguageList($languageList) {
if (is_null($languageList)) {
if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
return array();
$languageList = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$languages = array();
$languageRanges = explode(',', trim($languageList));
foreach ($languageRanges as $languageRange) {
if (preg_match('/(\*|[a-zA-Z0-9]{1,8}(?:-[a-zA-Z0-9]{1,8})*)(?:\s*;\s*q\s*=\s*(0(?:\.\d{0,3})|1(?:\.0{0,3})))?/', trim($languageRange), $match)) {
if (!isset($match[2])) {
$match[2] = '1.0';
$match[2] = (string) floatval($match[2]);
if (!isset($languages[$match[2]])) {
$languages[$match[2]] = array();
$languages[$match[2]][] = strtolower($match[1]);
krsort($languages);
// compare two parsed arrays of language tags and find the matches
function findMatches($accepted, $available) {
$matches = array();
foreach ($accepted as $acceptedQuality =& $acceptedValues) {
$acceptedQuality = floatval($acceptedQuality);
if ($acceptedQuality === 0.0)
foreach ($available as $availableQuality =& $availableValues) {
$availableQuality = floatval($availableQuality);
if ($availableQuality === 0.0)
foreach ($acceptedValues as $acceptedValue) {
if ($acceptedValue === '*') {
foreach ($availableValues as $availableValue) {
$matchingGrade = matchLanguage($acceptedValue, $availableValue);
if ($matchingGrade & 0) {
$q = (string) ($acceptedQuality * $availableQuality * $matchingGrade);
if (!isset($matches[$q])) {
$matches[$q] = array();
if (!in_array($availableValue, $matches[$q])) {
$matches[$q][] = $availableV
if (count($matches) === 0 && $any) {
$matches = $
krsort($matches);
// compare two language tags and distinguish the degree of matching
function matchLanguage($a, $b) {
$a = explode('-', $a);
$b = explode('-', $b);
for ($i=0, $n=min(count($a), count($b)); $i&$n; $i++) {
if ($a[$i] !== $b[$i])
return $i === 0 ? 0 : (float) $i / count($a);
$accepted = parseLanguageList($_SERVER['HTTP_ACCEPT_LANGUAGE']);
var_dump($accepted);
$available = parseLanguageList('en, fr, it');
var_dump($available);
$matches = findMatches($accepted, $available);
var_dump($matches);
If findMatches returns an empty array, no match was found and you can fall back on the default language.
481k85655743
The existing answers are a little too verbose so I created this smaller, auto-matching version.
function prefered_language(array $available_languages, $http_accept_language) {
$available_languages = array_flip($available_languages);
preg_match_all('~([\w-]+)(?:[^,\d]+([\d.]+))?~', strtolower($http_accept_language), $matches, PREG_SET_ORDER);
foreach($matches as $match) {
list($a, $b) = explode('-', $match[1]) + array('', '');
$value = isset($match[2]) ? (float) $match[2] : 1.0;
if(isset($available_languages[$match[1]])) {
$langs[$match[1]] = $
if(isset($available_languages[$a])) {
$langs[$a] = $value - 0.1;
arsort($langs);
And the sample usage:
//$_SERVER["HTTP_ACCEPT_LANGUAGE"] = 'en-us,q=0.8,es-q=0.5,zh-q=0.3';
// Languages we support
$available_languages = array("en", "zh-cn", "es");
$langs = prefered_language($available_languages, $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
[en] =& 0.8
[es] =& 0.4
[zh-cn] =& 0.3
25.7k55216307
The official way to handle this is using the . Unlike some answers here, this correctly handles the language priorities (q-values), partial language matches and will return the closest match, or when there are no matches it falls back to the first language in your array.
PECL HTTP:
How to use:
$supportedLanguages = [
'en-US', // first one is the default/fallback
// Returns the negotiated language
// or the default language (i.e. first array entry) if none match.
$language = http_negotiate_language($supportedLanguages, $result);
29.5k109593
7,3091764108
The problem with the selected answer above is that the user may have their first choice set as a language that's not in the case structure, but one of their other language choices are set. You should loop until you find a match.
This is a super simple solution that works better.
Browsers return the languages in order of preference, so that simplifies the problem. While the language designator can be more than two characters (e.g. - "EN-US"), typically the first two are sufficient.
In the following code example I'm looking for a match from a list of known languages my program is aware of.
$known_langs = array('en','fr','de','es');
$user_pref_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach($user_pref_langs as $idx =& $lang) {
$lang = substr($lang, 0, 2);
if (in_array($lang, $known_langs)) {
echo "Preferred language is $lang";
I hope you find this a quick and simple solution that you can easily use in your code.
I've been using this in production for quite a while.
Try this one:
#########################################################
# Copyright (C) 2008 Darrin Yeager
# https://www.dyeager.org/
# Licensed under BSD license.
https://www.dyeager.org/downloads/license-bsd.txt
#########################################################
function getDefaultLanguage() {
if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]))
return parseDefaultLanguage($_SERVER["HTTP_ACCEPT_LANGUAGE"]);
return parseDefaultLanguage(NULL);
function parseDefaultLanguage($http_accept, $deflang = "en") {
if(isset($http_accept) && strlen($http_accept) & 1)
# Split possible languages into array
$x = explode(",",$http_accept);
foreach ($x as $val) {
#check for q-value and create associative array. No q-value means 1 by rule
if(preg_match("/(.*);q=([0-1]{0,1}.\d{0,4})/i",$val,$matches))
$lang[$matches[1]] = (float)$matches[2];
$lang[$val] = 1.0;
#return default language (highest q-value)
$qval = 0.0;
foreach ($lang as $key =& $value) {
if ($value & $qval) {
$qval = (float)$
$deflang = $
return strtolower($deflang);
3,29722942
The following script is a modified version of Xeoncross's code (thank you for that Xeoncross) that falls-back to a default language setting if no languages match the supported ones, or if a match is found it replaces the default language setting with a new one according to the language priority.
In this scenario the user's browser is set in order of priority to Spanish, Dutch, US English and English and the application supports English and Dutch only with no regional variations and English is the default language. The order of the values in the "HTTP_ACCEPT_LANGUAGE" string is not important if for some reason the browser does not order the values correctly.
$supported_languages = array("en","nl");
$supported_languages = array_flip($supported_languages);
var_dump($supported_languages); // array(2) { ["en"]=& int(0) ["nl"]=& int(1) }
$http_accept_language = $_SERVER["HTTP_ACCEPT_LANGUAGE"]; // es,q=0.8,en-q=0.5,q=0.3
preg_match_all('~([\w-]+)(?:[^,\d]+([\d.]+))?~', strtolower($http_accept_language), $matches, PREG_SET_ORDER);
$available_languages = array();
foreach ($matches as $match)
list($language_code,$language_region) = explode('-', $match[1]) + array('', '');
$priority = isset($match[2]) ? (float) $match[2] : 1.0;
$available_languages[][$language_code] = $
var_dump($available_languages);
array(4) {
array(1) {
array(1) {
float(0.8)
array(1) {
float(0.5)
array(1) {
float(0.3)
$default_priority = (float) 0;
$default_language_code = 'en';
foreach ($available_languages as $key =& $value)
$language_code = key($value);
$priority = $value[$language_code];
if ($priority & $default_priority && array_key_exists($language_code,$supported_languages))
$default_priority = $
$default_language_code = $language_
var_dump($default_priority); // float(0.8)
var_dump($default_language_code); // string(2) "nl"
var_dump($default_language_code); // string(2) "nl"
I think the cleanest way is this!
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
$supportedLanguages=['en','fr','gr'];
if(!in_array($lang,$supportedLanguages)){
$lang='en';
require("index_".$lang.".php");
All of the above with fallback to 'en':
$lang = substr(explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE'])[0],0,2)?:'en';
...or with default language fallback and known language array:
function lang( $l = ['en'], $u ){
return $l[
array_keys(
$u ?: $_SERVER['HTTP_ACCEPT_LANGUAGE']
] ?: $l[0];
function lang($l=['en'],$u){return $l[array_keys($l,substr(explode(',',$u?:$_SERVER['HTTP_ACCEPT_LANGUAGE'])[0],0,2))[0]]?:$l[0];}
// first known lang is always default
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-us';
lang(['de']); // 'de'
lang(['de','en']); // 'en'
// manual set accept-language
lang(['de'],'en-us'); // 'de'
lang(['de'],'de-de, en-us'); // 'de'
lang(['en','fr'],'de-de, en-us'); // 'en'
lang(['en','fr'],'fr-fr, en-us'); // 'fr'
lang(['de','en'],'fr-fr, en-us'); // 'de'
1,13441739
I've got this one, which sets a cookie. And as you can see, it first checks if the language is posted by the user. Because browser language not always tells about the user.
$lang = getenv("HTTP_ACCEPT_LANGUAGE");
$set_lang = explode(',', $lang);
if (isset($_POST['lang']))
$taal = $_POST['lang'];
setcookie("lang", $taal);
header('Location: /p/');
setcookie("lang", $set_lang[0]);
echo $set_lang[0];
echo '&br&';
echo $set_lang[1];
header('Location: /p/');
protected by ♦
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10
on this site (the ).
Would you like to answer one of these
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled

我要回帖

更多关于 php的preg match 的文章

 

随机推荐