Skip to content

Instantly share code, notes, and snippets.

View grim-reapper's full-sized avatar

Imran Ali grim-reapper

View GitHub Profile
@AndrewChamp
AndrewChamp / autoloader.php
Last active December 24, 2018 06:11
PHP Autoloader - Includes files for classes that are instantiated.
<?php
spl_autoload_register(function($class){
$directorys = array(PATH.VERSION.MODULES, INSTALL.MODULES, INSTALL.THEME.'modules/');
foreach($directorys as $directory):
if(file_exists($directory.'class.'.$class.'.php')):
require($directory.'class.'.$class.'.php');
return;
endif;
endforeach;
@reinink
reinink / example.php
Last active October 23, 2020 15:45
Given multiple periods of time in a day, that may overlap, calculate the sum of all the periods, in hours.
<?php
$periods = [
[new DateTime('8AM'), new DateTime('5PM')], // 9 hours
[new DateTime('8AM'), new DateTime('5PM')], // 9 hours
[new DateTime('10AM'), new DateTime('12PM')], // 2 hours
[new DateTime('12PM'), new DateTime('5PM')], // 5 hours
[new DateTime('7PM'), new DateTime('8PM')], // 1 hour
[new DateTime('9AM'), new DateTime('4PM')], // 7 hours
];
@RadGH
RadGH / short-number-format.php
Last active February 25, 2025 21:33
Short Number Formatter for PHP (1000 to 1k; 1m; 1b; 1t)
<?php
// Converts a number into a short version, eg: 1000 -> 1k
// Based on: http://stackoverflow.com/a/4371114
function number_format_short( $n, $precision = 1 ) {
if ($n < 900) {
// 0 - 900
$n_format = number_format($n, $precision);
$suffix = '';
} else if ($n < 900000) {
@AndrewChamp
AndrewChamp / iframed.js
Last active December 24, 2018 06:11
iFrame'd Site
$(document).ready(function($) {
if(window.location !== window.parent.location){
$('header, footer').hide();
}
});
@kongondo
kongondo / remote-file-copy.php
Created January 14, 2016 13:01
Remote file copying with progress reporting in PHP.
<?php
/*
* Remote File Copy PHP Script 2.0.0
*
* Copyright 2012, Sebastian Tschan
* https://blueimp.net
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/MIT
*/
@gekh
gekh / damerau–levenshtein-distance.php
Last active July 19, 2023 17:07
Damerau–Levenshtein distance [PHP]
<?php
/**
* Find Damerau–Levenshtein distance between two string
*
* @param string $source
* @param string $dest
* @return int Damerau–Levenshtein distance
*/
function distance($source, $dest)
@jamielob
jamielob / upload.js
Created October 18, 2016 23:07 — forked from cmcewen/upload.js
Upload image from React Native to Cloudinary
var CryptoJS = require('crypto-js');
function uploadImage(uri) {
let timestamp = (Date.now() / 1000 | 0).toString();
let api_key = 'your api key'
let api_secret = 'your api secret'
let cloud = 'your cloud name'
let hash_string = 'timestamp=' + timestamp + api_secret
let signature = CryptoJS.SHA1(hash_string).toString();
let upload_url = 'https://api.cloudinary.com/v1_1/' + cloud + '/image/upload'
@AndrewChamp
AndrewChamp / .htaccess
Last active August 16, 2025 08:57
Force Port 80 or Port 443 via .htaccess. This will also remove www. from the url.
# FORCE PORT 80 (insecure / regular traffic)
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
###########################################################
#FORCE PORT 443 && REMOVE WWW. (secure / SSL)
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
@andrewlimaza
andrewlimaza / example.html
Created December 19, 2016 11:31
Print certain div / elements using window.print()
<script>
function printDiv(divName){
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
@azimidev
azimidev / calculate_profile_percentage.php
Last active July 11, 2022 01:51
Calculates how much percentage of a profile is completed. It stripes off timestamps like created_at updated_at and primary keys like ids. Usually beneficial when using Laravel
<?php
/**
* Calculate how much a profile is completed
*
* @param $profile
* @return float|int
*/
function calculate_profile($profile)
{
if ( ! $profile) {