Skip to content

Instantly share code, notes, and snippets.

View stemar's full-sized avatar

Steven Marshall stemar

View GitHub Profile
@stemar
stemar / encodeQueryString.js
Last active March 22, 2025 19:35
Encode data into a query string for a GET or POST request, in JavaScript ES5
function encodeQueryString(params, isPostRequest) {
var query = new Array();
for (var key in params) {
if (params.hasOwnProperty(key)) {
var value = params[key];
if (Object.prototype.toString.call(value) === "[object Array]") {
for (var i = 0, n = value.length; i < n; i++) {
query.push(encodeURIComponent(key + "[]") + "=" + encodeURIComponent(value[i]));
}
} else {
@stemar
stemar / http_query.js
Last active March 21, 2025 02:52
Create a http query string in ES6 JavaScript (no array in query string)
function http_query(data, arg_separator) {
var query = [];
for (var key in data) {
query.push(encodeURIComponent(key) + "=" + encodeURIComponent(data[key].toString()));
}
return array.join(arg_separator || "&");
}
@stemar
stemar / http_build_query.js
Last active March 21, 2025 14:50
PHP's http_build_query equivalent with RFC3986 for JavaScript ES5
function http_build_query(data, numeric_prefix, arg_separator, encoding_type_RFC1738) {
var i = 0, j = 0, encoded_key, encoded_val, query = [];
if (typeof arg_separator === "undefined") {
arg_separator = "&";
}
for (var key in data) {
if (data.hasOwnProperty(key)) {
var value = data[key];
if (Object.prototype.toString.call(value) === "[object Array]") {
for (var i = 0; i < value.length; i++) {
@stemar
stemar / br2nl.php
Created January 10, 2025 23:50
Reverse function of nl2br()
<?php
function br2nl($string) {
return preg_replace('/<br(\s*)?\/?>/i', PHP_EOL, $string);
}
@stemar
stemar / numeric_format.php
Last active April 19, 2023 05:18
Localize numeric value using encapsulated NumberFormatter functions
<?php
/**
* Localize numeric value using encapsulated NumberFormatter functions
*
* @param float $amount
* @param array $kwargs
* @link https://www.php.net/manual/en/numberformatter.format.php
* @link https://www.php.net/manual/en/numberformatter.create.php
* @link https://www.php.net/manual/en/class.locale.php
* @return string|false
@stemar
stemar / currency_format.php
Last active April 4, 2023 00:44
Localize currency using encapsulated NumberFormatter functions
<?php
/**
* Localize currency using encapsulated NumberFormatter functions
*
* @param float $amount
* @param array $kwargs
* @link https://www.php.net/manual/en/numberformatter.formatcurrency.php
* @link https://www.php.net/manual/en/numberformatter.create.php
* @link https://www.php.net/manual/en/class.locale.php
* @return string|false
@stemar
stemar / datetime_format.php
Last active April 4, 2023 00:44
Localize datetime using encapsulated IntlDateFormatter functions
<?php
/**
* Localize datetime using encapsulated IntlDateFormatter functions
*
* @param IntlCalendar|DateTimeInterface|array|string|int|float $datetime
* @param array $kwargs
* @link https://www.php.net/manual/en/intldateformatter.format.php
* @link https://www.php.net/manual/en/intldateformatter.create.php
* @link https://www.php.net/manual/en/class.locale.php
* @return string|false
@stemar
stemar / sanitize_input_array.php
Last active January 18, 2023 22:37
Filter out non-allowed parameters in the request input and protect parameter values against XSS.
<?php
/**
* Filter out non-allowed parameters in the request input and protect parameter values against XSS
*
* @param int $type INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, INPUT_ENV
* @param array $allowed_params
* @link https://www.php.net/manual/en/function.filter-input-array.php
* @return array
*/
function sanitize_input_array($type, array $allowed_params) {
@stemar
stemar / array_map_recursive.php
Created January 18, 2023 07:17
Recursive array_map()
<?php
function array_map_recursive($callback, $array) {
$func = function ($item) use (&$func, &$callback) {
return is_array($item) ? array_map($func, $item) : call_user_func($callback, $item);
};
return array_map($func, $array);
}
@stemar
stemar / mb_str_split.php
Last active February 10, 2025 15:47
Multibyte str_split(). The mbstring library PHP < 7.x doesn’t come with a multibyte equivalent of str_split(). This function behaves like mb_str_split in PHP >= 7.x
<?php
function mb_str_split($string, $length = 1, $encoding = NULL) {
if (!is_null($string) && !is_scalar($string)) {
$type = gettype($string) === 'object' ? get_class($string) : gettype($string);
throw new \Exception(sprintf('mb_str_split(): Argument #1 ($string) must be of type string, %s given', $type));
}
if ((!is_null($length) && !is_numeric($length)) || $length === '') {
$type = gettype($length) === 'object' ? get_class($length) : gettype($length);
throw new \Exception(sprintf('mb_str_split(): Argument #2 ($string) must be of type int, %s given', $type));
}