Skip to content

Instantly share code, notes, and snippets.

@pospi
pospi / detect-numeric-arrays.php
Created September 24, 2013 01:58
Determine if a PHP array is numeric in the quickest way available.
<?php
function isNumeric($arr)
{
return array_keys($arr) !== range(0, count($arr) - 1);
}
@pospi
pospi / string-bytelen.php
Created September 24, 2013 01:56
Get reliable byte length of a string in PHP. Works around an issue where the multibyte string extension can be configured to shadow strlen().
<?php
/**
* Works around an issue where the multibyte string extension
* can be configured to shadow strlen(), and no longer returns pure
* bytelength.
* @param string $str string to get byte length of
* @return int
*/
function bytelen($str)
{
@pospi
pospi / escaped-implode-explode.php
Created September 24, 2013 01:54
Like PHP's implode() and explode(), but lets you escape the delimiters for splitting the strings with.
<?php
/**
* Same as implode, but escape the separator when it exists in the array elements being imploded
* If the separator is longer than 1 character, only the first character need be escaped (not every character of the delimiter).
*
* @param string $sep delimiter to stich the array together with
* @param array $arr array to combine
* @param char $escape escape character to escape any found delimiters with
* @return imploded string
@pospi
pospi / font-list-bullets.css
Created September 24, 2013 01:18
List bullets using font characters (best used with icon fonts!). Supported in everything except IE6 & 7, with graceful fallback.
ul {
list-style:none;
padding: 0 0 0 2em; /* padding includes space for character and its margin */
/* IE7 and lower use default */
*list-style: disc;
*padding: 0 0 0 1em;
}
ul li:before {
content: '\25BA';
@pospi
pospi / fontsize-reset.less
Last active December 23, 2015 18:59
Setup base font size reliably with CSS. Creates any baseline font size you wish to base your em units off.
@BASE_FONT_SIZE = 16;
@BASE_LINE_HEIGHT = 1.5;
// consistent base font size & line height (16px)
html {
font-size: 100%;
*font-size: 16px;
line-height: @BASE_LINE_HEIGHT;
}
@pospi
pospi / placeholders.css
Last active December 23, 2015 18:59
Placeholder styles for all browsers. Note the opacity resets, as this is how the default placeholder styles are applied.
::-webkit-input-placeholder { /* WebKit browsers */
color: #F00;
opacity: 1;
}
:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
color: #F00;
opacity: 1;
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
color: #F00;
@pospi
pospi / simple-date-range.js
Created September 24, 2013 01:09
Simple method for determining the number of periods between two dates
/**
* usage: date/time date/time ms->sec->min->hour->day
* getPeriodBetween('1 Jul 2011', '26 Oct 2010', 1000 * 60 * 60 * 24);
*
* = number of days between these two dates
*/
function getPeriodBetween(date1, date2, timescale)
{
return (new Date(date1) - new Date(date2)) / (timescale);
}
@pospi
pospi / jquery-plugin-pattern.js
Created September 24, 2013 01:05
Boilerplate code for wrapping up a JavaScript object / class into a jQuery plugin
/**
* A jquery plugin wrapper for a pure JavaScript class
*
* This will automatically give us the following:
*
* - a constructor for our class using $('my.selector').myClass()
* - automatic method access to the class's instance functions via syntax
* $('my.selector').myClass('funcName', arg1, arg2, ...argN);
* - automatic property access of all the class's internal properties. Both this
* and the above can assist greatly with third-party code integration
@pospi
pospi / charset-converter.php
Created September 24, 2013 01:03
Class for working with data to be used in latin-1 encoded HTML documents, or for normalising user input.
<?php
class CharsetConverter
{
private static $WORD_CHARS_MAP = array(
"\xE2\x80\x9A" => "&sbquo;",
"\xE2\x80\x9E" => "&bdquo;",
"\xE2\x80\x98" => "'",
"\xE2\x80\x99" => "'",
"\xE2\x80\x9C" => "\"",
@pospi
pospi / jquery.clickoutside.js
Created September 24, 2013 00:54
Very simple jQuery event for handling unfocusing of elements when clicking outside of them.
(function($) {
var WATCH_FOCUS_ON = $();
$.event.special['clickoutside'] = {
setup: function()
{
WATCH_FOCUS_ON = WATCH_FOCUS_ON.add( this );
// bind document handler if this is the first guy being bound