Solved

PHP: Get language codes

Posted on 2010-08-31
3
205 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:Shinesh Premrajan
Shinesh Premrajan 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

895 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

14 Experts available now in Live!

Get 1:1 Help Now