?
Solved

PHP: Get language codes

Posted on 2010-08-31
3
Medium Priority
?
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 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 …
Suggested Courses

762 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