Way to get and edit GET variables in order to make hyperlinks

萝らか妹 提交于 2019-12-09 12:41:26

问题


Let's say that I have URL like somefile.php?sort=id&way=desc.

I want to write a function (or use already made one) that would let me add next variables to URL and set which I want to delete.

I thought about something like function editGetVar("$add","$leave") where $add would be array with new variables to add to URL and $leave would be array with variables that must stay in URL.

Example:

somefile.php?sort=id&way=desc&buyer=retailer

and I want to delete "buyer" and add "action", then the a href would look like this:

<a href="somefile.php?sort=id&way=desc&action=edit">

I would appreciate any ideas from you.


回答1:


Use http_build_query:

<?php
unset($_GET['buyer']);
$_GET['action'] = 'edit';

print '<a href="somefile.php?' . http_build_query($_GET) . '">!!</a>';
?>



回答2:


I believe you can split up the URI with $parts = parse_url($my_uri), manipulate the resulting array and stick it back together with the http_build_query function.




回答3:


Example:

$url = '?';
foreach( $_POST as $key => $value )
{
    $url .= $key . '=' . $value . '&';
}

You can add/edit a variable as:

$_GET[ 'sort' ] = 'asc';

You can delete as:

unlink( $_GET[ 'sort' ] );

You can wrap it into a function yourself ;)



来源:https://stackoverflow.com/questions/5183218/way-to-get-and-edit-get-variables-in-order-to-make-hyperlinks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!