Skip to content

Instantly share code, notes, and snippets.

@abbotto
abbotto / Multi-Fallback Script Loader for jQuery
Created March 5, 2012 20:28
A Multi-Fallback Script Loader for jQuery
/* A Multi-Fallback Script Loader for jQuery
By Jared S. Abbott */
if(typeof($) != 'function' || 'undefined'==typeof jQuery){document.write(unescape("%3Cscript src='//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js' type='text/javascript'%3E%3C/script%3E"));}
else if(typeof($) != 'function' || 'undefined'==typeof jQuery){document.write(unescape("%3Cscript src='http://ajax.microsoft.com/ajax/jquery/jquery-1.7.1.min.js' type='text/javascript'%3E%3C/script%3E"));}
else if(typeof($) != 'function' || 'undefined'==typeof jQuery){document.write(unescape("%3Cscript src='js/libs/jquery-1.7.1.min.js' type='text/javascript'%3E%3C/script%3E"));}
@abbotto
abbotto / display_source.php
Created October 26, 2012 16:00
Display the source code of a webpage.
<?
// Want to be able to display the source code of any webpage, with line numbering? Here is a simple code snippet to do it. Just modify the url on line 2 or make a function.
$lines = file('http://google.com/');
foreach ($lines as $line_num => $line) {
// loop thru each line and prepend line numbers
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br>\n";
}
@abbotto
abbotto / whois_query.php
Created October 26, 2012 16:01
Get Whois Information.
<?
// If you need to get the whois information for a specific domain, why not using PHP to do it? The following function take a domain name as a parameter, and then display the whois info related to the domain.
function whois_query($domain) {
// fix the domain name:
$domain = strtolower(trim($domain));
$domain = preg_replace('/^http:\/\//i', '', $domain);
$domain = preg_replace('/^www\./i', '', $domain);
$domain = explode('/', $domain);
$domain = trim($domain[0]);
@abbotto
abbotto / memory_usage.php
Created October 26, 2012 16:02
Get Info About Memory Usage.
<?
// In order to optimize your scripts, you may definitely want to know how many amount of RAM they use on your server. This snippet will check memory and then print initial, final and peak usages.
echo "Initial: ".memory_get_usage()." bytes \n";
/* prints
Initial: 361400 bytes
*/
// let's use up some memory
for ($i = 0; $i < 100000; $i++) {
@abbotto
abbotto / dominant_color.php
Created October 26, 2012 16:03
Get The Dominant Color Of Any Image.
<?
// Point the script to an image and get its dominant color.
$i = imagecreatefromjpeg("image.jpg");
for ($x=0;$x<imagesx($i);$x++) {
for ($y=0;$y<imagesy($i);$y++) {
$rgb = imagecolorat($i,$x,$y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> & 0xFF;
@abbotto
abbotto / crush.php
Created October 26, 2012 16:04
Compress Any Text-Based Content.
function crush($_file) {
$_file = preg_replace(array("/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/","!/\*[^*]*\*+([^/][^*]*\*+)*/!"), "", $_file); /* remove comments */
$_file = str_replace(array('\r\n','\r','\t','\n',' ',' ',' '), '', $_file); /* remove tabs, spaces, newlines, etc. */
/* remove spaces before/after certain characters */
$_file = preg_replace(array('(( )+\))','(\)( )+)'), ')', $_file); $_file = preg_replace(array('(( )+\()','(\(( )+)'), '(', $_file);
$_file = preg_replace(array('(( )+\})','(\}( )+)'), '}', $_file); $_file = preg_replace(array('(( )+\{)','(\{( )+)'), '{', $_file);
$_file = preg_replace(array('(( )+\:)','(\:( )+)'), ':', $_file); $_file = preg_replace(array('(( )+\;)','(\;( )+)'), ';', $_file);
$_file = preg_replace(array('(( )+\,)','(\,( )+)'), ',', $_file); return $_file;
}
@abbotto
abbotto / getElementsByClassName.php
Created December 5, 2012 04:42 — forked from flesch/getElementsByClassName.php
document::getElementsByClassName("")
<?php
class document {
public static function getElementsByClassName($name) {
global $xpath;
$nodes = array();
$elements = $xpath->query(sprintf("//div[contains(@class, '%s')]", $name));
foreach ($elements as $e) {
array_push($nodes, $e);
}
@abbotto
abbotto / LICENSE.txt
Created December 5, 2012 04:46 — forked from maettig/LICENSE.txt
getElementsByClassName in 140byt.es
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Thiemo Mättig <http://maettig.com/>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
@abbotto
abbotto / Snipplr-67838.txt
Created August 16, 2013 12:55
A function that forces a file to download.
function file_force_dl($_path) {
$mime = 'application/force-download';
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($_path).'"');
header('Content-Transfer-Encoding: binary');
header('Connection: close');
@abbotto
abbotto / Snipplr-67847.js
Created August 16, 2013 12:55
JavaScript: A function that uses images to perform a countdown.
<style type="text/css">
.countDown {
font-family:"Helvetica Neue",Helvetica,Calibri,"Liberation Sans",Arial,sans-serif;
font-weight:300;
letter-spacing:0.5px;
text-align:center;
}
</style>
<script type="text/javascript">
currentDate = new Date();