dream198731 2019-03-03 12:52
浏览 120
已采纳

php subdomain.domain假定为str_replace上的扩展名

I am trying to strip filename.extension from my local domain

local domain virtual host: http://cms.local/

and on str_replace I am trying to remove index.php to keep it dynamic my main concern is to consider the fact that filename.extension could be anything.

my code to do so:

private static function removeFilename($url) { $file_info = pathinfo($url); return isset($file_info['extension']) ? str_replace($file_info['filename'] . "." . $file_info['extension'], "", $url) : $url; }

where $url is unparsed url based on this post.

Considering that my url is http://cms.local/index.php everything is going smoothly but in case my url is just http://cms.local/; without filename.extension, str_replace function removes cms.local as if it is the filename.extension, leaving just http:///.

Thanks you in advance!

  • 写回答

1条回答 默认 最新

  • douwei9973 2019-03-03 13:25
    关注
    private static function removeFilename($url) {
        return substr($url, 0, strrpos($url, '/') + 1);
    }
    

    substr() Returns the portion of string specified by the start and length parameters.

    strrpos() Find the numeric position of the last occurrence of needle in the haystack string.

    This will not work if url is http://cms.local without the last slash.

    EDIT

    If you need to keep query string and remove just filename + extension:

    function removeFilename($url) {
        $queryString = parse_url($url, PHP_URL_QUERY);
        return substr($url, 0, strrpos($url, '/') + 1) . "?" . $queryString;
    }
    

    parse_url() function parses a URL and returns an associative array containing any of the various components of the URL that are present. The values of the array elements are not URL decoded.

    component

    Specify one of PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT to retrieve just a specific URL component as a string (except when PHP_URL_PORT is given, in which case the return value will be an integer).

    EDIT 2 Final solution with adding a trailing slash if url is http://cms.local

    private static function addTrailingSlash($url) {
        $url = parse_url($url);
        if(!isset($url['path'])) $url['path'] = '/';
        $surl = $url['scheme']."://".$url['host'].$url['path'].'?'.$url['query'];
        return $surl;   
    }
    
    private static function removeFilename($url) {
        $url = addTrailingSlash($url);
        $queryString = parse_url($url, PHP_URL_QUERY);
        return substr($url, 0, strrpos($url, '/') + 1) . "?" . $queryString;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名