Skip to content

Instantly share code, notes, and snippets.

View stemar's full-sized avatar

Steven Marshall stemar

View GitHub Profile
@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));
}
@stemar
stemar / mysqli_example.php
Last active September 14, 2022 21:15
Show MySQL types vs PHP types (MySQL formats) from a SELECT statement
<?php
// Connect
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect('localhost', 'root', '');
printf("Connected: %s\n", mysqli_get_host_info($mysqli));
// Create database
$database = 'mysqli_example';
$sql = sprintf("CREATE DATABASE IF NOT EXISTS %s", $database);
@stemar
stemar / SVN_workflow.md
Last active April 13, 2021 19:05
SVN commands and workflow
@stemar
stemar / path_join.php
Last active March 27, 2026 19:42
Normalize a path in PHP
<?php
/**
* Normalize a path.
*
* Usage: path_join('./one/', [], '/two/four', 0.0, ' / ', FALSE, 0, 'three', NULL, ' filename.php /');
* Result: './one/two/three/filename.php'
* Usage: path_join('/one/', [], '/two/four', 0.0, ' / ', FALSE, 0, 'three', NULL, ' filename.php /');
* Result: '/one/two/three/filename.php'
*/
function path_join(...$parts): string {
@stemar
stemar / vardump.php
Last active March 27, 2026 19:34
PHP var_dump() without newline after =>
<?php
/**
* PHP var_dump() without newline after => .
*
* NOTE: The only issue is when a string value has exactly `]=>\n\s+`, it will get converted to `] => `
* @link https://www.php.net/manual/en/function.var-dump.php
*/
function vardump(mixed $value, bool $print = TRUE) {
ob_start('mb_output_handler');
var_dump($value);
@stemar
stemar / is_blank.php
Last active February 28, 2026 07:20
Check if a value is blank but not zero.
<?php
/**
* Check if a value is blank but not zero.
* When you need to accept these as valid, non-empty values:
* - 0 (0 as an integer)
* - 0.0 (0 as a float)
* - "0" (0 as a string)
*/
function is_blank(mixed $value): bool {
return empty($value) && @!is_numeric($value);
@stemar
stemar / tidy_html5.php
Last active September 15, 2019 04:19
UTF-8 HTML5-compatible Tidy output
<?php
function tidy_html5($html, array $config = [], $encoding = 'utf8') {
$config += [
'doctype' => '<!DOCTYPE html>',
'drop-empty-elements' => 0,
'new-blocklevel-tags' => 'article aside audio bdi canvas details dialog figcaption figure footer header hgroup main menu menuitem nav section source summary template track video',
'new-empty-tags' => 'command embed keygen source track wbr',
'new-inline-tags' => 'audio command datalist embed keygen mark menuitem meter output progress source time video wbr',
'tidy-mark' => 0,
];
@stemar
stemar / long_datetime.php
Last active September 15, 2019 04:16
Format a datetime with a long format
<?php
function long_datetime($datetime, $level = 7) {
$now = new DateTime;
$datetime = new DateTime($datetime);
$suffix = $now > $datetime ? ' ago' : ' ahead';
$diff = $now->diff($dt);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$formats = [
'y' => ' year',