簡體   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