从php中删除一些可用的单词?

从php中删除一些可用的单词?,php,substring,Php,Substring,从php中删除一些可用的单词 例如,第一次访问页面 www.mysite.com/test.php?ABD_07,_oU_876.00/8999&message=success 从我的php代码中,我将获得$curreny_link_redirect=test.php?ABD_07、_oU_876.00/8999&message=success 我想得到$curreny_link_redirect_new=test.php?ABD_07,_oU_876.00/8999 剪切&消息=成功

从php中删除一些可用的单词

例如,第一次访问页面

www.mysite.com/test.php?ABD_07,_oU_876.00/8999&message=success
从我的php代码中,我将获得$curreny_link_redirect=test.php?ABD_07、_oU_876.00/8999&message=success

我想得到$curreny_link_redirect_new=test.php?ABD_07,_oU_876.00/8999

剪切&消息=成功

我该怎么办

<?PHP
    $current_link = "$_SERVER[REQUEST_URI]";
    $curreny_link_redirect = substr($current_link,1);
    $curreny_link_redirect_new = str_replace('', '&message=success', $curreny_link_redirect);
    echo $curreny_link_redirect_new;
?>
你喜欢这样吗

<?php
$str = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
echo $str = array_shift(explode('&',$str));
您的str_replace调用与它应该的调用相反。要替换的应该是第一个参数,而不是第二个参数

//Wrong
$curreny_link_redirect_new = str_replace('', '&message=success', $curreny_link_redirect);

//Right
$curreny_link_redirect_new = str_replace('&message=success','', $curreny_link_redirect);

也许不是答案,但对未来的访问者来说是免责声明:

1我强烈推荐函数:。 在这种情况下:

$current_link = "$_SERVER[REQUEST_URI]";
$arguments = explode('&', parse_url($current_link, PHP_URL_QUERY));
print_r($arguments);
2要构建新的url,请使用。我认为这是最好的、可以随时修改的解决方案

在这种情况下,这个解决方案有点过头了,但是这些函数确实很棒,值得在这里介绍

致以最诚挚的问候

试试这个:

$current_link_path = substr($_SERVER['PHP_SELF'], 1);
$params = $_GET;
if ($params['message'] == 'success') {
    unset($params['message']);
}
$current_link_redirect = $current_link_path . '?' . http_build_query($params);

虽然实现这一点的简单方法是使用正则表达式,甚至是带有str_replace的static,但我建议使用内置函数来处理url。这在处理复杂参数或多个参数时可能很有用:

$data = 'www.mysite.com/test.php?ABD_07,_oU_876.00/8999&message=success';
$url  = parse_url($data);
parse_str($url['query'], $url['query']);

//now, do something with parameters:
unset($url['query']['message']);
$url['query'] = http_build_query($url['query']);
$url  = http_build_url($url);

-请注意,准确地说,这是一个PECL函数PECL_http。上面的方法看起来可能更复杂,但它有好处——首先,正如我已经提到的,对于使用复杂参数或多个参数,这将很容易修改。其次,它将在结果中生成有效的url,即对斜杠、空格、e t.c.等进行编码。因此,结果总是正确的url。

您确定使用$current\u link=$\u SERVER[REQUEST\u URI]?它必须是$current\u link=$\u SERVER[REQUEST\u URI]@zerokavn实际上也应该是正确的。为什么不使用$\u GET数组而不是REQUEST\u URI?echo$\u GET['message']?而不是str\u replace,&message=success',$curreny\u link\u redirect;应为str_replace'&message=success',$curreny_link_redirect;回答得很好。我是第一个,但你的更完整+1 :.