Skip to content

Instantly share code, notes, and snippets.

@wuhei
wuhei / quickSortPHPSplFixedArray
Created May 5, 2023 12:58
Quicksort PHP SplFixedArray in PHP5.6, based on a key
/**
* Quicksorting (with partition) implementation to sort SplFixedArray on a specific key,
* I could not find this and I needed this because I'm stuck on PHP5.6 at work -- 2023-05-04 wuhei
*
* @param $arr SplFixedArray, array to sort
* @param $key mixed, array key to sort on
* @param $desc bool if true, sort descending
* @param $leftIndex int, left index of the array to sort, defaults to 0
* @param $rightIndex int, right index of the array to sort, defaults to count($arr) - 1
* @return mixed
@wuhei
wuhei / gist:3a62cf28e57be3c124db8f2ad62b7f57
Created July 25, 2024 08:43
check if ip (ipv4 or ipv6) is within a certain CIDR subnet
<?php
function ip_in_cidr($ip, $cidr) {
// Determine IP version with FILTER_VALIDATE_IP
$ip_version = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ? 4 :
(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ? 6 : false);
if (!$ip_version) {
throw new InvalidArgumentException("{$ip} is not a valid IP address");
}