Skip to content

Instantly share code, notes, and snippets.

View juji's full-sized avatar
💭
feeling awesome

Tri Rahmat Gunadi juji

💭
feeling awesome
View GitHub Profile
@juji
juji / dateToHuman
Created June 18, 2014 02:12
JS - MySQL timestamp to human readable
var dateToHuman = function(ds){
if(ds=='0000-00-00 00:00:00') return 'never';
ds = ds.replace(/\-/g,' ').replace(/\:/g,' ').split(' ');
var n = new Date();
var d = new Date(ds[0]*1,((ds[1]*1)-1),ds[2]*1,ds[3]*1,ds[4]*1,ds[5]*1);
var del = (n.valueOf() - d.valueOf())/1000;
if(del<60) return 'Just now';
var minute = Math.floor(del/60);
@juji
juji / deleteDirContent.php
Last active August 29, 2015 14:02
PHP - Delete directory content recursively
function delRec($dirPath){
foreach(
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$dirPath, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $path) {
$path->isDir() ? rmdir($path->getPathname()) : unlink($path->getPathname());
}
//uncomment to delete $dirPath
//rmdir($dirPath);
@juji
juji / .htaccess
Created June 18, 2014 03:16
Apache common .htaccess
Options -Indexes
Options +FollowSymLinks
IndexOptions +Charset=UTF-8
RewriteEngine On
# forced domain
RewriteCond %{HTTP_HOST} !=paragon.local
RewriteRule ^.*$ paragon.local%{REQUEST_URI} [R,L]
# Hotlink prevention
@juji
juji / errorControl.php
Last active August 29, 2015 14:02
PHP - error reporting control
error_reporting(E_ALL | E_STRICT);
// handle all error
ob_start(function($out){
$e = error_get_last();
if(!empty($e)) {
return $e['message'] . ' ( ' . $e['file'] . ' [' . $e['line'] . '] )';
}
return $out;
});
@juji
juji / cleanUTF.php
Last active August 29, 2015 14:02
PHP - clean unicode strings
function cleanUTF($some_string){
//reject overly long 2 byte sequences, as well as characters above U+10000 and replace with ?
$some_string = preg_replace('/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]'.
'|[\x00-\x7F][\x80-\xBF]+'.
'|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*'.
'|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})'.
'|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/S',
'', $some_string );
//reject overly long 3 byte sequences and UTF-16 surrogates and replace with ?
var cleanChar = function(str){
var f = [
String.fromCharCode(8220), //“
String.fromCharCode(8221), //”
String.fromCharCode(8216), //‘
String.fromCharCode(8217), //‘
String.fromCharCode(8211), //–
String.fromCharCode(8212), //—
String.fromCharCode(189), //½
String.fromCharCode(188), //¼
(function addXhrProgressEvent($) {
var originalXhr = $.ajaxSettings.xhr;
$.ajaxSetup({
progress: function() { console.log("standard progress callback"); },
xhr: function() {
var req = originalXhr(), that = this;
if (req) {
if (typeof req.addEventListener == "function") {
req.addEventListener("progress", function(evt) {
that.progress(evt);
@juji
juji / validateEmail.js
Last active August 29, 2015 14:02
JS - validate email
function validEmail(email){
return /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(email);
}
@juji
juji / regexReplace.js
Last active December 10, 2015 05:22
Replace regex special character
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
function toRegex(str,flag){
return new RegExp(escapeRegExp(str),flag);
}
@juji
juji / getDir.bash
Last active April 7, 2016 06:51
Bash get script directory
SOURCE="$0"
while [ -h "$SOURCE" ]; do
# resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
# if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
cd "$DIR"