简体   繁体   中英

Remove link after last Ampersand of URL in PHP

So I see split is no good anymore or should be avoided.

Is there a way to remove the LAST Ampersand and the rest of the link.

Link Before: http://www.websitehere.com/subdir?var=somevariable&someotherstuff&textiwanttoremove

Link After: http://www.websitehere.com/subdir?var=somevariable&someotherstuff

Right now I am using this script:

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

It takes the whole URL (all Ampersands included)...the issue is, the site the link is coming from adds a return value &RETURNVALUEHERE, I need to remove the last "&" and the rest of the text after it.

Thanks!

Robb

使用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, "&"));

Without knowing the Real URL, I was able to come up with this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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