Solved

PHP: Get language codes

Posted on 2010-08-31
3
204 Views
Last Modified: 2012-05-10
My code does what I need it to however it seems awkward to be using explode twice to get the language codes from my string.

Is there a better performing way to do this?
<?php

$LanguageHader = 'en-us,en;q=0.9,af;q=0.8,zu;q=0.7,ru;q=0.6,fr-ch;q=0.5,de-at;q=0.4,es-ec;q=0.3,sv;q=0.2,cy;q=0.1';
$langs=explode(',',$LanguageHader);
foreach ($langs as $lang){
 $l=explode(';',$lang);
 echo $l[0] . '<br />';
}

?>

Open in new window

0
Comment
Question by:hankknight
  • 2
3 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 33572084
No, it is not worth optimizing.  Make sure your data bases are indexed correctly.  I/O operations are several orders of magnitude slower than in-memory instructions.  
0
 
LVL 17

Assisted Solution

by:shinuq
shinuq earned 250 total points
ID: 33574454
<?php

$LanguageHader = 'en;q=0.9,af;q=0.8,zu;q=0.7,ru;q=0.6,fr-ch;q=0.5,de-at;q=0.4,es-ec;q=0.3,sv;q=0.2,cy;q=0.1';


$LanguageHader = 'en;q=0.9,af;q=0.8,zu;q=0.7,ru;q=0.6,fr-ch;q=0.5,de-at;q=0.4,es-ec;q=0.3,sv;q=0.2,cy;q=0.1';

$pattern="/[a-z]{2}\;[a-z]\=[0-9.]{3}/i";
preg_match_all($pattern,$LanguageHader,$cont);
echo "<pre>";
print_r($cont);
echo "</pre>";

}


Hope this helps
?>
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33577826
If the only language code you want is the first one, some kind of strpos() and substr() might work.  But whatever solution you're trying to optimize probably already completes in 0.00001 second or less.  So the optimization process is a little like milking a mouse - a lot of work for very little result!

best regards, ~Ray
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now