2

我想删除 $_SERVER['REQUEST_URI']; 输出的第一个文件夹

// I use it as language switcher in my website, here's how it works:

<?php $currenturl=$_SERVER['REQUEST_URI']; ?>

<a href="https://mysite.com/<?php echo $currenturl ?>">United States</a>
<a href="https://mysite.com/ca<?php echo $currenturl ?>">Canada</a>
<a href="https://mysite.com/br<?php echo $currenturl ?>">Brasil</a>

问题是,例如在加拿大,它输出:

https://mysite.com/ca/ca/mypage

它应该是

https://mysite.com/ca/mypage

4

4 回答 4

2

好吧,我认为您正在尝试获取路径的文件名,而不是删除第一个文件夹。试试下面的代码:

$currenturl=basename($_SERVER['REQUEST_URI']);
于 2013-11-21T06:25:43.907 回答
1

利用str_replace

<?php 

$currenturl=$_SERVER['REQUEST_URI'];
$currenturl=str_replace('/ca','',$currenturl); // I have added it here

?>

<a href="https://mysite.com/<?php echo $currenturl ?>">United States</a>
<a href="https://mysite.com/ca<?php echo $currenturl ?>">Canada</a>
<a href="https://mysite.com/br<?php echo $currenturl ?>">Brasil</a>
于 2013-11-21T06:25:29.370 回答
0

$_SERVER['REQUEST_URI']是由 php 提供的,它不是你可以简单地从你的 htaccess 文件中删除的东西。您需要在 php 中将其删除:

<?php $currenturl=preg_replace('#(/[a-z]{2})?(/.*)#','${2}',$_SERVER['REQUEST_URI']); ?>
于 2013-11-21T06:29:02.430 回答
0

我使用来自 Shankar Damodaran 的稍微修改的示例 -

<?php
    $currenturl = $_SERVER['REQUEST_URI'];
    $languge_folder  = array('/en', '/ru', '/ua');
    $currenturl_en = str_replace($languge_folder,'',$currenturl); 
    $currenturl_ru = str_replace($languge_folder,'',$currenturl);
    $currenturl_ua = str_replace($languge_folder,'',$currenturl);
?>
          <select id="select-language" onchange="window.location.href=this.value">
             <option value="/ua/<?php echo $currenturl_ua; ?>"  selected="selected">Ukrainian</option>
             <option value="/ru/<?php echo $currenturl_ru; ?>" >Russian</option>
             <option value="/en/<?php echo $currenturl_en; ?>" >English</option>
          </select>
于 2018-02-10T22:04:29.173 回答