繁体   English   中英

在PHP中URL的最后一个“&”之后删除链接

[英]Remove link after last Ampersand of URL in PHP

因此,我认为拆分已不再有用,应该避免。

有没有一种方法可以删除LAST与符号和其余的链接。

之前链接: http : //www.websitehere.com/subdir?var= somevariable&someotherstuff& textiwanttoremove

链接后: http : //www.websitehere.com/subdir? var=somevariable& someotherstuff

现在,我正在使用此脚本:

<?php
    $name = http_build_query($_GET);
    // which you would then may want to strip away the first 'name='
    $name = substr($name, strlen('name='));
    //change link to a nice URL
    $url = rawurldecode($name);
?>

<?php echo "$url"; ?>

它需要整个URL(包括所有的“&”符号)...问题是,链接来自的站点添加了返回值&RETURNVALUEHERE,我需要删除最后一个“&”以及其后的其余文本。

谢谢!

罗布

使用substrstrrpos

$url = substr($url, 0, strrpos($url, '&'));

如果您的输入已经是$ _GET,则删除最后一个值对可能就是这样:

http_build_query(array_slice($_GET, 0, -1));

你可以像这样使用strrpos()

$url = substr($orig_url, 0, strrpos($orig_url, "&"));

在不知道真实URL的情况下,我想到了这一点:

<?php

// The string:
$string = "http://www.websitehere.com/subdir?var=somevariable&someotherstuff&textiwanttoremove";

// get the position of the last "&"
$lastPos = strrpos($string, "&");

// echo out the final string:
echo substr($string, 0, $lastPos);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM