dongtao5104 2009-11-20 19:49
浏览 22
已采纳

PHP:解析匹配目录/ somedirectory /的URL并执行条件代码

This has to be pretty simple, but I'd like to parse the current URL and execute conditional code depending on whether the user is on the /sitemap/ directory.

So for example, if the site is example.com, and if the request is example.com/sitemap/.

Then I want to execute conditional code in that case. I'm using wordpress so I'm not sure if there is a built-in function that gets this...

A pure PHP solution is fine.

  • 写回答

6条回答 默认 最新

  • dsizmmwnm56437180 2009-11-20 19:56
    关注

    I don’t know if Wordpress has some function for that. But you could do this:

    $_SERVER['REQUEST_URI_PATH'] = preg_replace('/\\?.*/', '', $_SERVER['REQUEST_URI']);
    

    $_SERVER['REQUEST_URI'] contains the URI path plus the query and the code above will just get the URI path. Then you can compare it to your string:

    if ($_SERVER['REQUEST_URI_PATH'] == '/sitemap/') {
        // URI path is "/sitemap/"
    }
    

    Or you do a starts with test:

    $start = '/sitemap/';
    if (substr($_SERVER['REQUEST_URI_PATH'], 0, strlen($start)) == $start) {
        // URI path starts with "/sitemap/"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥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之后自动重连失效