Skip to content

Instantly share code, notes, and snippets.

@Erenor
Erenor / Countries iso-alpha-2 and iso-alpha-3 array
Created July 28, 2021 09:45
A simple array. indexed by iso-alpha-2, to convert country codes
$countries = array (
'AF' =>
array (
'alpha2' => 'AF',
'alpha3' => 'AFG',
'full' => 'Afghanistan',
),
'AL' =>
array (
'alpha2' => 'AL',
@Erenor
Erenor / names_generator_form_syllables.py
Last active February 16, 2020 15:15
[PYTHON 3.6] A simple class used to create random names based on syllables dictionaries
# Needed built-in modules
import random
# Randomic names for "whatever"
class MyRandomName:
"""Class used to create randomized names based on syllables-ish lists
Tested with Python 3.6
See: https://pyfiddle.io/fiddle/ea12ddcc-d989-4d87-9a62-836aa1e4b624/?i=true
"""
@Erenor
Erenor / function-random-string-to-hex-color.php
Last active February 16, 2020 15:15
[PHP] A simple function to get a 6-chars hexadecimal color starting from any string
/**
* Function takes a random string and returns an hex color in the 6-digits format
*
* @param string $input
*
* @return string
*/
function meow_str_to_hex_color($input) {
//characters that we do not want into an hex color
$replace = range('g', 'z');
@Erenor
Erenor / php-comparison-with-two-equal-signs.php
Created June 14, 2018 15:56
[PHP] The comparison with two equal signs
//to be used when you are not really sure about the outcome of a comparison, or when you get an "unexpected" result
$a = array( 'a', 0, '0', false, null, );
$b = $a;
foreach($a as $temp) {
foreach($b as $meow) {
echo var_export($temp, true) . ' == ' . var_export($meow, true) . ' : ' . var_export($temp == $meow, true);
echo '<br>';
}
}
@Erenor
Erenor / remove-duplicates-from-array.js
Last active May 23, 2018 07:17
[JAVASCRIPT] Function to remove duplicate items from an array of objects, using "key" as comparison (other keys are not considered)
/**
* Function to remove duplicate items, using "key" as comparison (other keys are not considered)
*/
function ereUniques(arr, key) {
var len = arr.length;
var seen = [];
var final = [];
var finalCounter = 0;
var i, innerItem;
for (i = 0; i < len; i++) {
@Erenor
Erenor / ereg-workaround-for-php-7.php
Last active September 28, 2022 08:16
[PHP] Workaround for the absent "ereg()" function, in PHP 7+
//make sure the function is not already there ;-)
if (!function_exists('ereg'))
{
//@see https://stackoverflow.com/questions/6270004/how-can-i-convert-ereg-expressions-to-preg-in-php
//@see http://php.net/manual/en/function.ereg.php
//@see http://php.net/manual/en/function.preg-match.php
function ereg($pattern, $string, $matches = null)
{
//quote delimiters, if already present in the pattern (ereg does not have delimiters)
$pattern = preg_quote($pattern, '~');
@Erenor
Erenor / countries-no-accents.php
Last active May 23, 2018 07:20
[PHP] Array of world countries (code => label), without special chars
<?php
//an array of countries indexed by their country code
//names do NOT contain special or accented chars
$countriesByCountryCode = array (
'AF' => 'Afghanistan',
'AX' => 'Aland Islands',
'AL' => 'Albania',
'DZ' => 'Algeria',
'AS' => 'American Samoa',
@Erenor
Erenor / italian-provinces.php
Last active May 22, 2019 09:48
[PHP] Array of italian provinces (code => label) ordered by province's name, with accents (e.g. "Forlì") and single quotes (e.g. "L'Aquila") where needed
<?php
//we use double quotes to avoid having to escape the single quote within the city of L'Aquila
//accents are not html encoded: use UTF8 to ensure they will not be transformed to something strange
$provinces = array (
'AG' => "Agrigento",
'AL' => "Alessandria",
'AN' => "Ancona",
'AO' => "Aosta",
'AR' => "Arezzo",
'AP' => "Ascoli Piceno",