Skip to content

Instantly share code, notes, and snippets.

View stemar's full-sized avatar

Steven Marshall stemar

View GitHub Profile
@stemar
stemar / encode_query.php
Last active March 30, 2026 18:31
Encode the query of a URL with RFC3986.
<?php
function encode_query(string $url): string {
if ($query = parse_url($url, PHP_URL_QUERY)) {
[$path] = explode($query, $url, 2);
parse_str($query, $data);
$query = http_build_query($data, "", NULL, PHP_QUERY_RFC3986);
if ($fragment = parse_url($url, PHP_URL_FRAGMENT)) {
$fragment = '#'.rawurlencode($fragment);
}
return join([$path, $query, $fragment]);
@stemar
stemar / filter_sanitize_string.php
Last active February 16, 2026 04:16
Polyfill for FILTER_SANITIZE_STRING deprecated as of PHP 8.1.0
<?php
function filter_sanitize_string($value, $flags = 0) {
if ($flags & FILTER_FLAG_EMPTY_STRING_NULL && $value === "") {
return null;
}
if (!(is_scalar($value) || is_null($value))) {
return false;
}
// Strip HTML tags and remove NUL bytes
@stemar
stemar / array_column.php
Last active March 27, 2026 23:59
Polyfill for array_column() pre-PHPv5.5
<?php
if (!function_exists('array_column')) {
function array_column(array $array, $column_key, $index_key = NULL) {
$output = array();
foreach ($array as $row) {
if (is_null($index_key)) {
$output []= is_null($column_key) ? $row : $row[$column_key];
}
else {
$output[$row[$index_key]] = is_null($column_key) ? $row : $row[$column_key];
@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 / format_number.php
Last active March 28, 2026 00:06
Localize number value using encapsulated NumberFormatter functions
<?php
/**
* Localize number value using encapsulated NumberFormatter functions
*
* @param float $amount
* @param array $args
* @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
* @link https://unicode.org/reports/tr35/tr35-numbers.html#table-number-pattern-character-definitions
@stemar
stemar / format_currency.php
Last active March 28, 2026 00:06
Localize currency using encapsulated NumberFormatter functions
<?php
/**
* Localize currency using encapsulated NumberFormatter functions
*
* @param float $amount
* @param array $args
* @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
* @link https://unicode.org/reports/tr35/tr35-numbers.html#table-number-pattern-character-definitions
@stemar
stemar / format_datetime.php
Last active July 5, 2025 20:16
Localize datetime using encapsulated IntlDateFormatter functions
<?php
/**
* Localize datetime using encapsulated IntlDateFormatter functions
*
* @param IntlCalendar|DateTimeInterface|array|string|int|float $datetime
* @param array $args
* @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
* @link https://unicode-org.github.io/icu/userguide/format_parse/datetime/#date-field-symbol-table