Manipulate a url string by adding GET parameters

前端 未结 15 2153
旧巷少年郎
旧巷少年郎 2020-11-29 00:50

I want to add GET parameters to URLs that may and may not contain GET parameters without repeating ? or &.

Example:

If I want t

15条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 01:15

    Here's a shorter version of the accepted answer:

    $url .= (parse_url($url, PHP_URL_QUERY) ? '&' : '?') . 'category=action';
    

    Edit: as discussed in the accepted answer, this is flawed in that it doesn't check to see if category already exists. A better solution would be to treat the $_GET for what it is - an array - and use functions like in_array().

提交回复
热议问题