0

我们的网站有两种语言版本,捷克语和英语。我们在每个页面的右上角(在 header.php 中)有一个简单的标志,它指向捷克索引的 index_cz.php 和所有页面的英语索引 index.php,无论当前 URL 是什么。

<a href="index_cz.php" target="_self"><img src="img/Czech-icon.png" width="64" height="64" alt="Czech language version"></a>
<a href="index.php" target="_self"><img src="img/United-Kingdom-icon.png" width="64" height="64" alt="English language version">

我想要使​​用 PHP 的更复杂的东西,它可以根据当前页面的 URL 更改按钮的 URL。例如:如果当前页面是“new.php”,我希望捷克语按钮现在指向“new_cz.php”,而英文按钮是“new.php”。

包含?id=结尾的页面(具有与该页面关联的任何 id)使用单独的 header.php 并从数据库中引用适当的 id,例如:

<a href="project_cz.php?id=<?php echo $det[0];?>" target="_self"><img src="img/Czech-icon.png" width="64" height="64" alt="Czech language version"></a>    
<a href="project.php?id=<?php echo $det[0];?>" target="_self"><img src="img/United-Kingdom-icon.png" width="64" height="64" alt="English language version"></a>

你能想出合适的代码来处理标准的 php 页面吗?非常感谢!

4

1 回答 1

2

您可以使用 访问当前文件$_SERVER['PHP_SELF'],但请确保htmlspecialchars在回显之前对其运行以防止 XSS 攻击。

也就是说,您可以 str_replace 当前文件以获取捷克语版本:

str_replace('.', '_cz.', $_SERVER['PHP_SELF'])

另一种方法也很简单:

str_replace('_cz.', '.', $_SERVER['PHP_SELF'])
于 2011-06-28T18:08:36.433 回答