dongqiao8502 2012-05-04 12:33
浏览 39
已采纳

使用PHP获取URL的一部分

I have a site and I would need to get one pages URL with PHP. The URL might be something www.mydomain.com/thestringineed/ or it can www.mydomain.com/thestringineed?data=1 or it can be www.mydomain.com/ss/thestringineed

So it's always the last string but I dont want to get anything after ?

  • 写回答

6条回答 默认 最新

  • duanang58939 2012-05-04 12:41
    关注

    You will use the parse_url function, then look at the path portion of the return. like this:

    $url='www.mydomain.com/thestringineed?data=1';
    $components=parse_url($url);
    
    //$mystring= end(explode('/',$components['path']));
    
    // I realized after this answer had sat here for about 3 years that there was 
    //a mistake in the above line
    // It would only give the last directory, so if there were extra directories in the path, it would fail. Here's the solution:
    $mystring=str_replace( reset(explode('/',$components['path'])),'',$components['path']); //This is to remove the domain from the beginning of the path.
    
    // In my testing, I found that if the scheme (http://, https://, ...) is present, the path does not include 
    //the domain. (it's available on it's own as ['host']) In that case it's just  
    // $mystring=$components['path']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端