我需要检测用户使用的语言,以使用PHP包含正确的文件,如果elseif或类似的话:
用户来自:
example.com/EN/nice-title-url-from-database-slug
example.com/DE/nice-title-url-from-database-slug
example.com/ES/nice-title-url-from-database-slug
我需要的php是这样的:
PHPdocument.location.toString((.split(…(等检测url路径
if url path <starts with /DE/>
include de.php
elseif url <path starts with /EN/>
include en.php
else url <path starts with /ES/>
include es.php
所以我需要的是在域(/ES/或/EN/或/DE/(之后检测url
知道如何做到这一点吗?
怎么样
$check = "example.com/EN/";
if (substr($url, 0, strlen($check)) === $check) { ... }
为了实现我们想要的,我们需要:
- 查找当前页面的URL-我们可以使用$_SERVER['REQUEST_URI'](在PHP中获取完整的URL
- 由此,我们想弄清楚它是否包含语言部分。这样做的一种方法是,按照您的建议拆分字符串,并获得第二个结果(即键1(:爆炸('/',$_SERVER['REQUEST_URI'](1
- 然后我们可以做include或您需要的逻辑
所以下面是我的建议。
// Get the uri of the request.
$uri = $_SERVER['REQUEST_URI'];
// Split it to get the language part.
$lang = explode('/', $uri)[1]; // 1 is the key - if the language part is placed different, this should be changed.
// Do our logic.
if ($lang === 'DE') {
include 'de.php';
} else if ($lang === 'EN') {
include 'en.php';
} else if ($lang === 'ES') {
include 'es.php';
}
要获取页面URL,请使用$_SERVER['REQUEST_URI']
。然后使用/
分解以获取URL的不同部分。
$parts = explode('/',$_SERVER['REQUEST_URI']);
$parts
现在包含以下元素。
Array
(
[0] =>
[1] => EN
[2] => nice-title-url-from-database-slug?bla
)
正如您所看到的,$parts
数组的索引1就是您所需要的。