Solved

PHP: Get language codes

Posted on 2010-08-31
3
207 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 109

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 109

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

810 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