douluanzhao6689 2015-01-11 19:18
浏览 20
已采纳

从URL末尾删除字符

Say I have a URL with something like this:
http://website.com/website/webpage/?message=newexpense

I have the following code to try and get the the URL before the question mark:

$post_url = $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$link_before_question_mark = explode('?', $actual_link);
$add_income_url = $link_before_question_mark[0];

In this example I would get the following URL:
http://website.com/website/webpage/

I'd like to remove the webpage portion of this so the URL is:
http://website.com/website/

How can I do this?

  • 写回答

4条回答 默认 最新

  • dqo58772 2015-01-11 19:30
    关注

    You can do a similar trick using explode. Then pop the parts you don't need and implode the url back together. If you are sure that the part after '?' never contains a '/', you can replace your code with this one. If you're not sure, you should first remove the part after '/' and then run this code to remove the last part of the path.

    <?php
    $url = 'http://website.com/website/webpage/?message=newexpense';
    
    $parts = explode('/', $url);
    
    // Remove the last part from the array
    $lastpart = array_pop($parts);
    
    // If the last part is empty, or the last part starts with a '?'
    // this means there was a '/' at the end of the url, so we 
    // need to pop another part.
    if ($lastpart == '' or substr($lastpart, 0, 1) == '?')
      array_pop($parts);
    
    $url = implode('/', $parts);
    
    var_dump($url);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 根据以下文字信息,做EA模型图
  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥60 关机时蓝屏并显示KMODE_EXCEPTION_NOT_HANDLED,怎么修?
  • ¥66 如何制作支付宝扫码跳转到发红包界面