Skip to content

Instantly share code, notes, and snippets.

@dlueth
dlueth / .htaccess
Created June 13, 2013 12:14
Cross domain font embedding for Firefox via Apache .htaccess
<FilesMatch "\.(ttf|otf|eot|woff|svg)$">
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
</FilesMatch>
@dlueth
dlueth / .htaccess
Created June 13, 2013 12:16
Unset all cookies via Apache .htaccess
Header unset Cookie
Header unset Set-Cookie
@dlueth
dlueth / loop.for.i.js
Last active January 2, 2018 15:25
JavaScript: Best practice "for i" loop
var values = [ 'entry 1', 'entry 2', 'entry 3' ], i, value;
for(i = 0; (value = entry[i]) !== undefined; i++) {
// do whatever you want in here :)
}
@dlueth
dlueth / arguments.convert.array.js
Last active January 2, 2018 15:25
JavaScript: Convert a functions array-like arguments to array
/*
* Works on e.g.: arguments (cross-browser)
* Does not work on e.g.: NodeList like from "document.getElementsByTagName"
*/
function foo() {
var args = [].slice.call(arguments, 0);
}
@dlueth
dlueth / isValidFilename.php
Created June 27, 2013 21:27
PHP validator that checks if a given string is a valid/allowed filename across operating systems
function isValidFilename($value) {
return (strlen($value) <= 255 && preg_match('/^(?!^(PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d|\..*)(\..+)?$)[^\x00-\x1f\?*:"";|\/]+$/', $value) > 0);
}
@dlueth
dlueth / isValidPath.php
Created June 27, 2013 21:28
PHP validator that checks if a given string is a valid/allowed filesystem path across operating systems
function isValidPath($value) {
return (preg_match('/^(\/(?:(?:(?:(?:[a-zA-Z0-9\\-_.!~*\'():\@&=+\$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*)(?:;(?:(?:[a-zA-Z0-9\\-_.!~*\'():\@&=+\$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*))*)(?:\/(?:(?:(?:[a-zA-Z0-9\\-_.!~*\'():\@&=+\$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*)(?:;(?:(?:[a-zA-Z0-9\\-_.!~*\'():\@&=+\$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*))*))*))$/', (string) $value) > 0);
}
@dlueth
dlueth / transliterate.php
Created June 27, 2013 21:29
PHP modifier to transliterate a given string (requires iconv, mb_string)
function transliterate($value, $characterset) {
return iconv(mb_detect_encoding($value, NULL, true), $characterset . '//IGNORE//TRANSLIT', trim($value));
}
@dlueth
dlueth / sluggify.php
Created June 27, 2013 21:31
PHP modifier to sluggify (make url-safe) any given string
function sluggify($value) {
return trim(strtolower(preg_replace('/([^\w]|-)+/', '-', trim(strtr(str_replace('\'', '', trim($value)), array(
'À'=>'A','Á'=>'A','Â'=>'A','Ã'=>'A','Å'=>'A','Ä'=>'A','Æ'=>'AE',
'à'=>'a','á'=>'a','â'=>'a','ã'=>'a','å'=>'a','ä'=>'a','æ'=>'ae',
'Þ'=>'B','þ'=>'b','Č'=>'C','Ć'=>'C','Ç'=>'C','č'=>'c','ć'=>'c',
'ç'=>'c','Ď'=>'D','ð'=>'d','ď'=>'d','Đ'=>'Dj','đ'=>'dj','È'=>'E',
'É'=>'E','Ê'=>'E','Ë'=>'E','è'=>'e','é'=>'e','ê'=>'e','ë'=>'e',
'Ì'=>'I','Í'=>'I','Î'=>'I','Ï'=>'I','ì'=>'i','í'=>'i','î'=>'i',
'ï'=>'i','Ľ'=>'L','ľ'=>'l','Ñ'=>'N','Ň'=>'N','ñ'=>'n','ň'=>'n',
'Ò'=>'O','Ó'=>'O','Ô'=>'O','Õ'=>'O','Ø'=>'O','Ö'=>'O','Œ'=>'OE',
@dlueth
dlueth / sanitize.php
Created June 27, 2013 21:32
PHP modifier to sanitize (remove any hostile characters) from any given string
function sanitize($value) {
$value = preg_replace('/[\x00-\x1f\?*:";|\/°^!§$%&\\()=´`+#\':,<>]/', '', trim($value));
$value = preg_replace('/^(?:PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d)(?:\.*)(.*)/', '\1', $value);
return $value;
}
@dlueth
dlueth / convertCharset.php
Created June 27, 2013 21:33
PHP modifier to convert the encoding of any given string (requires mb_string)
function convertCharset($value, $characterset = NULL) {
return (mb_check_encoding($value, $characterset) === true) ? $value : mb_convert_encoding($value, $characterset);
}