PHP获取URL字符串参数?

时间:2019-01-14 16:16:30

标签: php html

在这里阅读许多文章之后,我没有找到解决方案,因此我需要帮助才能执行此操作... 我的网址是那些 示例:

主页

https://mywebsite.com/
https://mywebsite.com/al/
https://mywebsite.com/it/
https://mywebsite.com/videos/
https://mywebsite.com/al/videos/
https://mywebsite.com/it/videos/
https://mywebsite.com/news/
https://mywebsite.com/al/news/
https://mywebsite.com/it/news/

查询

https://mywebsite.com/search/?q=YouTube
https://mywebsite.com/videos/search/?q=YouTube
https://mywebsite.com/news/search/?q=YouTube

https://mywebsite.com/al/search/?q=YouTube
https://mywebsite.com/al/videos/search/?q=YouTube
https://mywebsite.com/al/news/search/?q=YouTube

https://mywebsite.com/it/search/?q=YouTube
https://mywebsite.com/it/videos/search/?q=YouTube
https://mywebsite.com/it/news/search/?q=YouTube

我的php和html更改语言

<?php $Ava_Sulg = $_SERVER["REQUEST_URI"];?>

<a class="x" href="/<?php echo $Ava_Sulg;?>">EN</a>
<a class="x" href="/al<?php echo $Ava_Sulg;?>">AL</a>
<a class="x" href="/it<?php echo $Ava_Sulg;?>">IT</a>

所以我允许用户更改语言,我想做的是,当他们更改语言时,URL可以是上述之一,例如,如果他们从AL to IT和url中更改语言是https://mywebsite.com/al/videos/search/?q=YouTubePHP,我想获得这个https://mywebsite.com/it/videos/search/?q=YouTube,所以我想从仅此URL(/al/ to /it/)更改,或者从IT to EN({{1 }}),但是我想更改的内容是在中间,并且首页上的内容有所不同,这对我来说非常困难,这是可能的还是没有办法?如果可能,我希望在这里找到解决方案!非常感谢你。

6 个答案:

答案 0 :(得分:6)

由于您只对操作 path 组件感兴趣,因此可以简单地使用parse_url提取路径组件而不是regex:

function generateurl($url, $lc) {
    $parts = parse_url($url);
    $parts['path'] = preg_replace('@^/[a-z][a-z]/@', '/', $parts['path']);
    if ($lc !== 'en') {
        $parts['path'] = '/' . $lc . $parts['path'];
    }
    $url = '';
    if (isset($parts['scheme'])) $url .= $parts['scheme'] . '://';
    if (isset($parts['host'])) $url .= $parts['host'];
    if (isset($parts['port'])) $url .= ':' . $parts['port'];
    if (isset($parts['path'])) $url .= $parts['path'];
    if (isset($parts['query'])) $url .= '?' . $parts['query'];
    if (isset($parts['fragment'])) $url .= '#' . $parts['fragment'];
    return $url;
}

如果您有http_build_url功能可用,则可以简化上述功能。功能输入输出:

                           https://mywebsite.com/ + en = https://mywebsite.com/
                           https://mywebsite.com/ + al = https://mywebsite.com/al/
                           https://mywebsite.com/ + it = https://mywebsite.com/it/
                        https://mywebsite.com/al/ + en = https://mywebsite.com/
                        https://mywebsite.com/al/ + al = https://mywebsite.com/al/
                        https://mywebsite.com/al/ + it = https://mywebsite.com/it/

                    https://mywebsite.com/videos/ + en = https://mywebsite.com/videos/
                    https://mywebsite.com/videos/ + al = https://mywebsite.com/al/videos/
                    https://mywebsite.com/videos/ + it = https://mywebsite.com/it/videos/
                 https://mywebsite.com/al/videos/ + en = https://mywebsite.com/videos/
                 https://mywebsite.com/al/videos/ + al = https://mywebsite.com/al/videos/
                 https://mywebsite.com/al/videos/ + it = https://mywebsite.com/it/videos/

   https://mywebsite.com/videos/search/?q=YouTube + en = https://mywebsite.com/videos/search/?q=YouTube
   https://mywebsite.com/videos/search/?q=YouTube + al = https://mywebsite.com/al/videos/search/?q=YouTube
   https://mywebsite.com/videos/search/?q=YouTube + it = https://mywebsite.com/it/videos/search/?q=YouTube
https://mywebsite.com/al/videos/search/?q=YouTube + en = https://mywebsite.com/videos/search/?q=YouTube
https://mywebsite.com/al/videos/search/?q=YouTube + al = https://mywebsite.com/al/videos/search/?q=YouTube
https://mywebsite.com/al/videos/search/?q=YouTube + it = https://mywebsite.com/it/videos/search/?q=YouTube

                        /videos/search/?q=YouTube + en = /videos/search/?q=YouTube
                        /videos/search/?q=YouTube + al = /al/videos/search/?q=YouTube
                        /videos/search/?q=YouTube + it = /it/videos/search/?q=YouTube
                     /al/videos/search/?q=YouTube + en = /videos/search/?q=YouTube
                     /al/videos/search/?q=YouTube + al = /al/videos/search/?q=YouTube
                     /al/videos/search/?q=YouTube + it = /it/videos/search/?q=YouTube

答案 1 :(得分:4)

首先,我建议您将英语/en/保留在URL中,这样会更易于管理。

然后提取语言代码并替换为您可以使用的其他值

您可以使用preg_replace (REGEX)

$url = "https://mywebsite.com/en/foo";
$codes = [ 'it', 'fr', ...]; 
$urls = [];
foreach($codes as $code){
    $urls[$code] = preg_replace("(https://mywebsite.com/)[a-z]{2}(.*)", "$1". $code. "$2", $url);
}

答案 2 :(得分:2)

我创建了一个为您执行此操作的函数。运作方式如下:

  1. 它会从网址中删除所选语言及其后的所有内容,并将其放入$startUrl中。
  2. 获取原始网址,并删除其中的第一部分(包括语言),并将其放入$endUrl中。
  3. 将提供的语言添加到中间,并将所有三个部分放在一起,将其命名为$newUrl并返回。
  

注意:此功能要求,即使在英语中,您也要在每个url中指定语言!

     

注2:如果将基本URL的结构从https://mywebsite.com/更改为{{3},则可能需要在函数的两个删除部分中都更改$key值}。我在代码中添加了有关它们位于何处的注释。

function createUrl($url, $language){
    /*
    * FIX THE BEGINNING OF THE URL
    */
    // Explode the url into smaller parts and put into an array
    foreach((explode('/', $url)) as $key => $value){
        $expArray[$key] = $value;
    };

    // Remove the last part of the URL (including chosen language)
    foreach($expArray as $key => $value){
        if($key > 0){ /*<--This is one of the values you might need to be changed if your base url structure changes*/
            unset($expArray[$key]); 
        }
    }

    // Implode the array back to a string
    foreach($expArray as $key => $value){
        $startUrl = implode('/', $expArray);
    };



    /*
    * FIX THE END OF THE URL
    */
    // Explode the url into smaller parts and put into an array
    foreach((explode('/', $url)) as $key => $value){
        $expArray[$key] = $value;
    };

    // Remove the first part of the URL (including chosen language)
    foreach($expArray as $key => $value){
        if($key < 2){ /*<--This is the other value you might need to be changed if your base url structure changes*/
            unset($expArray[$key]); 
        }
    }

    // Implode the array back to a string
    foreach($expArray as $key => $value){
        $endUrl = implode('/', $expArray);
    };


    /* 
    * Put it all together
    */
    if(isset($endUrl)){
       $newUrl = $startUrl . $language . $endUrl;
       return $newUrl;
    }else{
       $newUrl = $startUrl . $language;
       return $newUrl;
    }
};

要在示例中使用它,应这样编写:

<a class="x" href="<?php echo createUrl($_SERVER["REQUEST_URI"], '/EN/');?>"></a>
<a class="x" href="<?php echo createUrl($_SERVER["REQUEST_URI"], '/AL/');?></a>
<a class="x" href="<?php echo createUrl($_SERVER["REQUEST_URI"], '/IT/');?></a>

答案 3 :(得分:2)

替换URL是不好的做法。您应该将使用URL分为两部分:

  1. 没有语言的语言和URI解析
  2. 带有语言的任何URI的构建

例如,语言解析可能看起来像:

function extractLanguage($uri)
{
    if (preg_match('/^\/[a-z]{2}\//', $uri, $matches)) {
        $language = trim($matches[0], '/');
    } else {
       $language = ''; // or default language
    }   
    return $language;
}

extractLanguage('/search/?q=YouTube'); // will return empty string
extractLanguage('/al/search/?qURIuTube'); // will return 'al'

没有语言的URI解析看起来像

function extractUri($uri)
{
    $uri= preg_replace('/^\/[a-z]{2}\//', '', $uri);
    if ($uri[0] !== '/') {
        $uri = '/' . $uri;
    }
    return $uri;
}

extractUri('/search/?q=YouTube'); // will return '/search/?q=YouTube'
extractUri('/al/search/?q=YouTube'); // will return '/search/?q=YouTube'

如果您将使用单独的语言和单独的URI,则可以构建目标URL,例如借助以下功能

function buildUri($path, $params = [], $language = '')
{
   $query = '';
   if (!empty($params)) {
       $query = '?' . http_build_query($params);
   }
   if (!empty($language)) {
       $language = '/' . $language ;
   } 
   return $language . $path . $query;
}

buildUri('/news/search', array('q' => 'YouTube')) // will return '/news/search/?q=YouTube'
buildUri('/news/search', array('q' => 'YouTube'), 'it') // will return 'it/news/search/?q=YouTube'

答案 4 :(得分:1)

我的解决方案!如果用户在https://example.com/es/news/search/?q=news上搜索语言时(例如从es更改为en),URL像这样https://example.com/search/?q=news

更改,则“不完美”
if(isset($_GET["q"])) {
$qurl = $_GET["q"];
$surl = "/search/?q=";
}else{
$qurl = "";
$surl = "";
}

答案 5 :(得分:0)

我建议在Apache Web服务器以使用。htaccess配置, 在其中您可以处理所有类型的请求很容易地隐藏参数, 并且您可以轻松地重定向到其他网址。

以下网址将为您提供帮助。

http://www.htaccess-guide.com/-官方文档

https://help.dreamhost.com/hc/en-us/articles/215747748-How-can-I-redirect-and-rewrite-my-URLs-with-an-htaccess-file--htaccess示例