Php 从URL末尾删除字符

Php 从URL末尾删除字符,php,Php,假设我有这样一个URL: http://website.com/website/webpage/?message=newexpense 我有以下代码尝试在问号之前获取URL: $post_url = $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $link_before_question_mark = explode('?', $actual_link); $add_income_url = $link_be

假设我有这样一个URL:
http://website.com/website/webpage/?message=newexpense

我有以下代码尝试在问号之前获取URL:

$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];
在本例中,我将获得以下URL:
http://website.com/website/webpage/

我想删除此文件的网页部分,因此URL为:
http://website.com/website/


我怎样才能做到这一点?

您可以使用
explode
进行类似的操作。然后弹出您不需要的部分,然后
内爆
url重新组合在一起。如果您确定“?”之后的部分从不包含“/”,则可以使用此部分替换代码。如果不确定,应首先删除“/”之后的部分,然后运行此代码删除路径的最后部分

<?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);

您可以使用
分解
执行类似的技巧。然后弹出您不需要的部分,然后
内爆
url重新组合在一起。如果您确定“?”之后的部分从不包含“/”,则可以使用此部分替换代码。如果不确定,应首先删除“/”之后的部分,然后运行此代码删除路径的最后部分

<?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);
尝试使用explode

<?php
$actual_link = "http://website.com/website/webpage/?message=newexpense]";
$link_before_question_mark = explode('?', $actual_link);
$add_income_url = $link_before_question_mark[0];
$split=explode('/',  $add_income_url);
echo $split[0]."//".$split[2]."/".$split[3]."/";

?>

更好的是

<?php
$actual_link = "http://website.com/website/webpage/?message=newexpense]";
$split=explode('/',  $actual_link);
echo $split[0]."//".$split[2]."/".$split[3]."/";

?>

尝试使用explode

<?php
$actual_link = "http://website.com/website/webpage/?message=newexpense]";
$link_before_question_mark = explode('?', $actual_link);
$add_income_url = $link_before_question_mark[0];
$split=explode('/',  $add_income_url);
echo $split[0]."//".$split[2]."/".$split[3]."/";

?>

更好的是

<?php
$actual_link = "http://website.com/website/webpage/?message=newexpense]";
$split=explode('/',  $actual_link);
echo $split[0]."//".$split[2]."/".$split[3]."/";

?>

我可能会使用;它是专门设计用来去除“/”之后的最后一件东西

(正如文档中所说,“dirname()对输入字符串的操作非常简单,并且不知道实际的文件系统…”,因此用于这种目的是非常安全的。)

我可能会使用;它是专门设计用来去除“/”之后的最后一件东西

(正如文档中所说,“dirname()对输入字符串的操作非常简单,并且不知道实际的文件系统…”,因此用于这种用途是非常安全的。)

这样使用,您就拥有了所有组件

$url = 'http://website.com/website/webpage/?message=newexpense';
$pUrl = parse_url( $url);
echo $pUrl['scheme'] . '://' . $pUrl['host'] . $pUrl['path'];
使用这种方式,您将拥有所有组件

$url = 'http://website.com/website/webpage/?message=newexpense';
$pUrl = parse_url( $url);
echo $pUrl['scheme'] . '://' . $pUrl['host'] . $pUrl['path'];

是的,解析url是分离事物的好方法。请注意,在这种情况下,您仍然需要去掉“网页”部分,以便从问题中获得所需的URL。同意。这是你还需要做什么的问题。如果我有一个包含查询的uri,那么我希望它能在脚本中的某个时候被使用,这样就可以一次性获得所有可用的东西。但是如果没有其他用途,那么dirname肯定能很好地完成任务。是的,parse_url是一种很好的分离方式。请注意,在这种情况下,您仍然需要去掉“网页”部分,以便从问题中获得所需的URL。同意。这是你还需要做什么的问题。如果我有一个包含查询的uri,那么我希望它能在脚本中的某个时候被使用,这样就可以一次性获得所有可用的东西。但如果没有其他用途,那么dirname肯定能很好地完成任务。