图上代码实现:获取URL,如http://localhost/a_1/c_2 或 http://localhost/a_1/c 或 http://localhost/a_1  都进行 "_" 分割, http://localhost/a_1/c_2 执行后=http://localhost/a/c  
http://localhost/a_1/c 执行后=http://localhost/a/c  
 http://localhost/a 执行后=http://localhost/a/c  求高手们,看看这段代码有没有冗余的代码,或有什么改进的地方。

解决方案 »

  1.   


    重写URL?你说的是apache那个ReWrite的吧?
      

  2.   

    是的。。apache也可以。。nginx也可以.....
      

  3.   


    我知道可以用rewrite来重写URL,问题是这个,不是固定的方式,而且有些链接不同意,我在wordpress里面需要_来区分多语言页面。
      

  4.   

    如果我没理解错的话,你需要的是这样的效果
    $ar = array(
      'http://localhost/a_1/c_2',
      'http://localhost/a_1/c',
      'http://localhost/a_1',
    );foreach($ar as $url) {
      $t = parse_url($url);
      $p = preg_replace('#_.+(/|$)#U', '$1', $t['path']);
      echo "$t[scheme]://$t[host]$p" . PHP_EOL;
    }http://localhost/a/c
    http://localhost/a/c
    http://localhost/a