更改语言而不离开当前页面

时间:2013-03-14 16:00:22

标签: php

我正在尝试更改语言但保留在当前页面上: 即:

www.myurl.com/english/aboutus.php

www.myurl.com/german/aboutus.php

www.myurl.com/french/aboutus.php

因此只有语言目录发生变化。 我有以下但无法让它工作:

<?php 
$full_url = $_SERVER['FULL_URL'] ;  

$temparray = explode(".com/",$full_url,2); 

$englishtemp = $temparray[0] . ".com/english/" . $temparray[1]; 
$englishlink = "<a href=$englishtemp><img src=../images/english-flag.jpg></a>"; 

echo $englishlink; 
?>

我将url从'/french/aboutus.php'更改为'/ english',但它不记得页面名称'aboutus.php'并返回索引页。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

使用basename($_SERVER['PHP_SELF'])作为当前页面。

$englishtemp = "/english/" . basename($_SERVER['PHP_SELF']); 
$englishlink = "<a href=$englishtemp><img src=../images/english-flag.jpg></a>";
echo $englishlink;

请参阅PHP documentation on $_SERVER

答案 1 :(得分:0)

试试这个。它将生成除当前所选语言之外的所有语言的链接:)

<?php
$full_url = $_SERVER['REQUEST_URI']; //select full url without the domain name
$languages = array('english', 'french', 'german'); //array of all supported languages

$url_bits = explode('/', $full_url); //divide url into bits by /
$current_language = $url_bits[1]; //current language is held in the second item of the array

//find current language in the array and remove it so we wouldn't have to display it
unset($languages[array_search($current_language, $languages)]);

$links = '';

foreach ($languages as $language) {
    //generate links with images for the rest of the languages
    $links .= '<a href="/' . $language . '/' . $url_bits[2] . '"><img src="../images/' . $language . '-flag.jpg" title="'.ucfirst($language).'" /></a>';
}

echo $links;
?>