0

I'm having an issue removing a particular query string from a url. I want to keep the rest of the queries but simply dump the last one. The problem is the query string could have a different value each time, so I dont know how to do this.

For example my site url might be:

http://sitename.com/index.php?Cat=Bus&Origin=UK

What I want to do, is keep Cat=Bus and remove Origin=UK

So if I try to change the origin to Germany I write:

echo '<a href="'.$_SERVER['REQUEST_URI'].'&Origin=Germany">Germany</a>';

So it takes the page it is on:

http://sitename.com/index.php?Cat=Bus&Origin=UK

Using Request_URI. I want to then strip it of &Origin=* Then once thats done pass it into a string and add whatever the new origin is on the end.

I figure:

$theURI = $_SERVER['REQUEST_URI'];

But I have no idea how to go from start to finish.

$amended = $theURI - '&Origin=*'

and then end up with

echo '<a href="'.$amended.'&Origin=Germany">Germany</a>';

I just don't know how to express that $amended function in PHP.

Could somebody help? This is being used for sorting between countries

4

2 に答える 2

1

オリジンの取得方法 (静的 URL と動的 URL) に応じて、$_GETスーパーグローバルを簡単に操作できます。

$url = $_SERVER['REQUEST_URI']; //edit: I usually use $_SERVER['PHP_SELF'], so try this if REQUEST_URI fails
$get_param = $value; //$value can be germany, england, poland, etc etc
$url .= "&Origin=".$get_param;
echo "<a href='".$url."'>$get_param</a>";

これが役立つことを願っています

于 2012-12-15T09:35:01.603 に答える
1

正確な変数を知っていて、それらがそれほど多くない場合は、Dale のコメントを使用できます。

echo '<a href="/index.php?Cat=' . $_GET['Cat'] . '&Origin=Germany">Germany</a>';

そうでない場合は、str_replace を使用できます。

$url = $_SERVER['REQUEST_URI'];
$url = str_replace("origin=" . $_GET["origin"], "", $url);
$url = preg_replace("#&+#", "&", $url);
... 
echo "<a href='" . $url . "&origin=Germany"'>Germany</a>";
于 2012-12-15T09:46:55.357 に答える