Skip to content

Instantly share code, notes, and snippets.

@krmgns
krmgns / String.format.js
Created February 18, 2015 16:02
Simple string formatter with integer and float operators.
String.prototype.format = function() {
var string = this.toString();
if (arguments.length) {
var tokens = string.match(/%([sdf])/g) || [],
token, i = 0, replace;
while (token = tokens.shift()) {
replace = arguments[i++];
switch (token) {
case "%d":
string = string.replace(token, parseInt(replace, 10) || 0);
@krmgns
krmgns / git-memo
Last active August 29, 2015 14:15
Git memo
# delete a remote tag
git tag -d theTagName
git push origin :refs/tags/theTagName
@krmgns
krmgns / sublime-stuff.md
Last active August 29, 2015 14:14
Sublime Stuff...
@krmgns
krmgns / HTML.tmLanguage
Last active November 12, 2015 16:49
Sublime theme
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>fileTypes</key>
<array>
<string>html</string>
<string>htm</string>
<string>shtml</string>
<string>xhtml</string>
var Class = (function() {
return {
create: function(prototype){
function Class() {
if (this.init && this.init.apply) {
this.init.apply(this, arguments);
}
}
Class.prototype = prototype;
Class.prototype.constructor = Class;
@krmgns
krmgns / reduce_slashes.php
Last active August 29, 2015 14:13
Reduce duplicated slashes.
<?php
$url = "https://foo.com//bar/////ii?a=1";
$url = preg_replace("~(?<!:)/+~", "/", $url);
print($url); # https://foo.com/bar/ii?a=1
$str = <<<EOT
Lorem [a href="#"]ipsum[/a] [hr color="red" /] dolor...
[style].foo{color:#fff}[/style]
EOT;
function bbcode_convert($content) {
// remove style|script
$content = preg_replace(
'~(\[(style|sctript)\s?.*\](.*)\[/(\\2)\]|\[(%s)\s?.*/\])~ims', '', $content);
@krmgns
krmgns / .htaccess
Last active August 29, 2015 14:13
Proper .htaccess configrations for subdir-based virtual server (Apache/2.4.7 (Ubuntu)).
<VirtualHost *:80>
ServerName foo.com.local
DocumentRoot /var/www/foo.com/public
<Directory /var/www/foo.com/public>
Options +FollowSymLinks
DirectoryIndex index.php
AllowOverride all
Require all granted
function extract_array_path($path, $value = null, $base = null) {
if ($base === null) {
static $base = array();
}
$exp = explode('.', $path);
$tmp =& $base;
foreach($exp as $i) {
$tmp =& $tmp[$i];
}
// Handles also array params well
function parseQueryString(query) {
var pars = (query != null ? query : "").replace(/&+/g, "&").split('&'),
par, key, val, re = /^([\w]+)\[(.*)\]/i, ra, ks, ki, i = 0,
params = {};
while ((par = pars.shift()) && (par = par.split('=', 2))) {
key = decodeURIComponent(par[0]);
// prevent param value going to be "undefined" as string
val = decodeURIComponent(par[1] || "").replace(/\+/g, " ");