Skip to content

Instantly share code, notes, and snippets.

View emmadesilva's full-sized avatar
🎩
HydePHP.com

Emma De Silva emmadesilva

🎩
HydePHP.com
View GitHub Profile
@emmadesilva
emmadesilva / getNumberOfDaysInYear.php
Created May 7, 2024 09:11
Get the numbers of days in a year
<?php
function getNumberOfDaysInYear(int $year): int
{
// Creates a Unix timestamp for January 1st of the given year to check if it's a leap year
$isLeapYear = date('L', mktime(0, 0, 0, 1, 1, $year));
return $isLeapYear ? 366 : 365;
}
@emmadesilva
emmadesilva / deterministic-php-array-random-function.php
Last active May 7, 2024 08:59
Deterministic function to extract random items from an array. The same seed will result in the same items being extracted every time. Now updated with a similar deterministic array shuffle function to randomize an array order.
<?php
/**
* Deterministic function to extract random items from an array.
*
* The same seed will result in the same items being extracted every time.
*/
function extractRandomItems(array $array, int $count, int $seed): array
{
mt_srand($seed); // Set seed for deterministic randomness
<?php
// PHP has a number of built-in functions that allow you to perform
// linguistic operations on strings with knowledge of phonetics,
// as well as the English language rules and pronunciations.
// Calculate the metaphone key of a string
echo metaphone('test'); // TST
// Calculate the soundex key of a string
<?php
use App\Helpers\TextFormatter;
$array = ['Apple', 'Banana', 'Orange'];
// Awesome! Very readable and concise
array_map([TextFormatter::class, 'capitalize'], $array);
// Verbose, but classic, also adds cognitive load by introducing a new function
@emmadesilva
emmadesilva / capture-composer-output.php
Last active April 18, 2024 17:53
Capture Composer output
<?php
// As Composer writes to stderr, we need to capture the output manually
$command = 'composer global require hyde/cli';
// read from the stderr stream
$descriptorspec = [
2 => ['pipe', 'w'],
];
<?php
// Set the user agent as required by the GitHub API
ini_set('user_agent', 'github.com/your-username');
// Now you can make requests to the GitHub API
$response = file_get_contents('https://api.github.com/zen');
die($response); // Speak like a human.
<?php
/** @return array{major: int, minor: int, patch: int} */
function parseSemver(string $semver): array
{
return array_combine(['major', 'minor', 'patch'],
array_map('intval', explode('.', $semver))
);
}
<?php
function printSerializedArray(array $data): string
{
$string = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
// Convert JSON to PHP array syntax
$string = str_replace(['{', '}'], ['[', ']'], $string);
$string = str_replace('": "', "' => '", $string);
$string = str_replace('": [', "' => [", $string);
<?php
/**
* Get all of the given array without the specified array of values.
*
* @param array $array
* @param array|string|int|float $values
* @return array
*/
public static function without($array, $values)
@emmadesilva
emmadesilva / weighted-element.php
Created April 6, 2024 12:37
PHP weighted array selection
<?php
function weightedElement(array $array, float $factor = 1.5): mixed {
$totalWeight = 0;
$weightedArray = [];
// Reverse the array
$array = array_reverse($array);
// Calculate the total weight of the elements