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条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改