2

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

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”并返回到索引页面。

有人可以帮忙吗?

4

2 回答 2

2

用于basename($_SERVER['PHP_SELF'])当前页面。

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

请参阅$_SERVER 上的 PHP 文档

于 2013-03-14T16:14:08.343 回答
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;
?>
于 2013-03-14T16:25:25.290 回答