[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP: Get language codes

Posted on 2010-08-31
3
Medium Priority
?
214 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 111

Accepted Solution

by:
Ray Paseur earned 1000 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:Shinesh Premrajan
Shinesh Premrajan earned 1000 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 111

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month19 days, 3 hours left to enroll

834 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