PHP检查URL并检测空参数部分

时间:2018-06-19 06:01:06

标签: php parameters

我有这样的多语言系统的Url结构:

表示默认语言,即:en

localhost/cms

和另一种语言一样:

localhost/cms/fr

localhost/cms/de

localhost/cms/es

现在我有了这个函数来检测空的url语言短名称部分并设置为默认语言:

function urlss(){

    $uri = $_SERVER['REQUEST_URI'];

    $languages = ['en', 'fr', 'es', 'de'];

    // this is my default language
    $defaultLang = 'en';

    // $currentLang is the language I'll use to show the contents
    $currentLang = $defaultLang;

    $uri = ltrim(rawurldecode($uri), '/');
    $parts = explode('/', $uri);

    if( ! empty($parts) && in_array(strtolower($parts[0]), $languages)) {
        $currentLang = array_shift($parts);
    }

    $routableUri = implode('/', $parts);

return $routableUri;

}

签入网址:localhost/cms/,输出为:cms/,但无效。

在行动中我需要检查我的网址,如果没有检测到语言短名称,即:en默认语言= en,然后输入sessioncookie但是如果检测语言短名称即:fr es desessioncookie设置为此语言。

如何修复问题并检查我的网址是否为短名称?!

4 个答案:

答案 0 :(得分:1)

我会使用正则表达式解决这个问题。

// Prepare request URI, available languages and default language
$url = $_SERVER['REQUEST_URI'];
$languages = ['en', 'fr', 'es', 'de'];
$default = 'en';

// Build regular expression and attempt to match language in URL
preg_match('/^\/cms\/(' . implode('|', $languages) . ')\//', $url, $match);

// Set language to match or default to English
$language = $match[1] ?? $default;

// Remove default language from URL
$url = preg_replace("/\/{$default}/", '', $url);

答案 1 :(得分:0)

正则表达式方法更好,您可以将$languages数组与网址$parts交叉。

function urlss(){

    $uri = $_SERVER['REQUEST_URI'];

    $languages = ['en', 'fr', 'es', 'de'];

    // this is my default language
    $defaultLang = 'en';

    // $currentLang is the language I'll use to show the contents
    $currentLang = $defaultLang;

    $uri = ltrim(rawurldecode($uri), '/');
    // uri might have many / characters
    $parts = explode('/', $uri);

    if( ! empty($parts)) {
        $result = array_uintersect($languages , $parts, "strcasecmp");
        if(!empty($result)){ 
           $currentLang = $result[0];
           $parts = array_diff($parts, [ $result[0] ]);
        }
    }

    $routableUri = implode('/', $parts);

    return $routableUri;

}

答案 2 :(得分:0)

$languages = ['en', 'fr', 'es', 'de'];
$defaultLang = 'en';

$url = $_SERVER['REQUEST_URI'];
$url = rtrim($url, '/');//remove last slash if available
$parsed_arr = parse_url($url);//not necessary if you don't want to validate url 
$segments = explode("/", $parsed_arr['path']);//split parsed url into segments
$language = end($segments); //take the last one

if(in_array($language,$languages))
    $currentLang = $language;
else
    $currentLang = $defaultLang;     

答案 3 :(得分:0)

让我们试试这段代码:

查看list()

休息你可以根据需要轻松改变,但我已经纠正了功能的逻辑

<?php
   $currentUrls= array('cms/es','cms/en','cms/de','cms/fr','cms/');
   echo 'Before::<br >';
   foreach($currentUrls as $currentUrl){
      echo ($currentUrl);
     echo '<hr/>';
   }

function urlss($currentUrl){
    $uri =$currentUrl;
    $languages = ['en', 'fr', 'es', 'de'];
    // this is my default language
   $defaultLang = 'en';
   // $currentLang is the language I'll use to show the contents
   $currentLang = $defaultLang;
   $uri = ltrim(rawurldecode($uri), '/');       
   list($cms,$url_language) = explode('/', $uri);     

  //if langugage is in array then 
  if( in_array( strtolower($url_language), $languages)){
      $currentLang = $url_language;      
  }else { 
      $currentLang = $defaultLang;      
  }

  $newUrl = array($cms,$currentLang);
  $routableUri = implode('/', $newUrl);
  return $routableUri;
 }

 echo '<br ><br >After::<br >';
 foreach($currentUrls as $currentUrl){
    echo urlss($currentUrl);
    echo '<hr/>';
 }

&GT;

enter image description here