Would it be better to convert the array in the following code to
$langs = array("en", "de", "fr");
and then reusing the values for both $folder
and $flag
? If so, how then would my foreach (or maybe a while?) loop be written?
<?php
$langs = array(
"en/" => "en.png",
"de/" => "de.png",
"fr/" => "fr.png"
);
$self = $_SERVER['REQUEST_URI'];
$pattern = "{^.*/}i";
$links = array();
foreach ($langs as $folder => $flag) {
$url = preg_replace($pattern, "$folder", $self);
$link = "<li><a href=\"../$url\"><img src=\"../img/$flag\"></a></li>";
array_push($links, $link);
}
echo implode($links) . "\n";
?>
I'm trying to "fool proof" the code, by effectively limiting the folder structures and file names that can be used, as I create a basic template of files for quick rolling out of our websites. If you can see any other improvements, that would be much appreciated.