nyholm/append-query-string

一个简单的函数,允许您向现有URL添加查询字符串

1.0.0 2021-08-16 01:31 UTC

This package is auto-updated.

Last update: 2024-09-16 07:59:17 UTC


README

如果您有一个未知的URL并且想要向其中添加查询字符串,这个包就是您要找的。

安装

composer require nyholm/append-query-string

使用方法

$url = 'https://nyholm.tech?example=yes';
$queryString = http_build_query(['foo'=>'bar']);

$result = append_query_string($url, $queryString);

echo $result;
// https://nyholm.tech?example=yes&foo=bar

是的,这几乎就像写作一样

$result = $url . $queryString;

但它将支持URL是否有查询字符串。它还将支持URL使用哈希片段。

模式

您可以使用三种不同的模式与append_query_string一起使用。

  • APPEND_QUERY_STRING_IGNORE_DUPLICATE(默认)
  • APPEND_QUERY_STRING_REPLACE_DUPLICATE
  • APPEND_QUERY_STRING_SKIP_DUPLICATE

它们可以通过示例最容易地解释。

APPEND_QUERY_STRING_IGNORE_DUPLICATE

$url = 'https://nyholm.tech?foo=x&a=1';
$queryString = http_build_query(['a'=>'2']);

$result = append_query_string($url, $queryString, APPEND_QUERY_STRING_IGNORE_DUPLICATE);

echo $result;
// https://nyholm.tech?foo=x&a=1&a=2

APPEND_QUERY_STRING_REPLACE_DUPLICATE

$url = 'https://nyholm.tech?foo=x&a=1';
$queryString = http_build_query(['a'=>'2']);

$result = append_query_string($url, $queryString, APPEND_QUERY_STRING_REPLACE_DUPLICATE);

echo $result;
// https://nyholm.tech?foo=x&a=2

APPEND_QUERY_STRING_SKIP_DUPLICATE

$url = 'https://nyholm.tech?foo=x&a=1';
$queryString = http_build_query(['a'=>'2']);

$result = append_query_string($url, $queryString, APPEND_QUERY_STRING_SKIP_DUPLICATE);

echo $result;
// https://nyholm.tech?foo=x&a=1