Giter Club home page Giter Club logo

append_query_string's Introduction

PHP function to append query string to URL.

If you have a unknown URL and want to add a query string to it, this package is what you are looking for.

Install

composer require nyholm/append-query-string

Usage

$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

Yes, this is pretty much as writing:

$result = $url . $queryString;

But it will support if URL has query string or not. It will also if a URL hash fragment is used.

Modes

There are three different modes you can use with append_query_string.

  • APPEND_QUERY_STRING_IGNORE_DUPLICATE (default)
  • APPEND_QUERY_STRING_REPLACE_DUPLICATE
  • APPEND_QUERY_STRING_SKIP_DUPLICATE

They are easiest explained with examples.

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

append_query_string's People

Contributors

carlos-ea avatar nikograno avatar nyholm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

append_query_string's Issues

PHP 8.0 Support

New version of PHP has been released. Currently composer.json doesn't allow you to use this package with PHP 8.0.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.