【问题标题】:PHP: Remove 'print/' from URL from end of a StringPHP:从字符串末尾的 URL 中删除 'print/'
【发布时间】:2013-02-13 09:37:48
【问题描述】:

此代码提供了 QR 码的 URL,但我将在页面的打印版本上使用它,因此我必须(preg_replace)从 url 字符串的末尾删除 'print/',所以给出的 URL 用于非打印版本。请。

public function getPageUrl() {      
    $url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    return $url;
}

【问题讨论】:

    标签: php string url preg-replace


    【解决方案1】:
    public function getPageUrl() {
        $url = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    
        if (empty($_SERVER['HTTPS'])) {
            $url = str_replace('https://', 'http://', $url);
        }
    
        return preg_replace('~/print/?$~i', '', $url);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-05-11
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 2011-03-07
      • 1970-01-01
      • 1970-01-01
      • 2012-12-13
      • 2013-05-24
      相关资源
      最近更新 更多