我需要檢測用戶使用的語言,以使用 PHP if 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 是這樣的:
PHP document.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
知道如何實作這一目標嗎?
uj5u.com熱心網友回復:
關于什么
$check = "example.com/EN/";
if (substr($url, 0, strlen($check)) === $check) { ... }
?
uj5u.com熱心網友回復:
為了實作我們想要的,我們需要:
- 查找當前頁面的 URL - 我們可以使用 $_SERVER['REQUEST_URI'] (獲取 PHP 中的完整 URL
- 由此,我們想弄清楚它是否包含語言部分。一種方法是按照您的建議拆分字串,并獲得第二個結果(即鍵 1):explode('/', $_SERVER['REQUEST_URI']) 1
- 然后我們可以做包含或你需要的邏輯。
所以以下將是我的建議。
// 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';
}
uj5u.com熱心網友回復:
要獲取頁面 URL,請使用$_SERVER['REQUEST_URI']. 然后使用explode by/
來獲取URL 的不同部分。
$parts = explode('/',$_SERVER['REQUEST_URI']);
$parts現在包含以下元素。
Array
(
[0] =>
[1] => EN
[2] => nice-title-url-from-database-slug?bla
)
如您所見,$parts陣列的索引 1 是您所需要的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/520169.html
標籤:php网址
下一篇:正則運算式從url中洗掉相對路徑
