I have the following PHP code that replaces folder names in the URL structure, for easy switching of languages whilst staying on the current page:
<?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";
?>
However, I've realised that with a simple array reading $langs = array("en", "de", "fr"); I could possibly save a few bytes and simplify the bottom-half of the code, reusing the variables for the folder and for the image? Would this be a better way of doing things? And how?
This is what I have so far, but the $langs['$x'] seems wrong? (In fact, I get a syntax error on my loop conditions!):
$langs = array("en", "de", "fr");
$n = count($langs); // 3
while ($x = 0; $x < $n; $x++ ) {
$folder = $langs['$x'] . "/";
$flags = $langs['$x'] . ".png";
echo $folder . " " . $flags; // test
}