Skip to content

Instantly share code, notes, and snippets.

View lsauer's full-sized avatar
🎯
Focusing

Lorenz Lo Sauer lsauer

🎯
Focusing
View GitHub Profile
@lsauer
lsauer / php array merge by value
Created September 6, 2011 07:35
PHP: Array_merge by value, or array merge by values and keys
/**
L. Sauer,2011
license: public domain
http://xhr2.blogspot.com/2011/09/php-arraymerge-by-value-or-array-merge.html
*/
function array_merge_values_explode(){
$args = func_get_args();
$elem = array();
$delim = strlen(end($args))<=2 ? array_pop($args) : ',';
@lsauer
lsauer / gist:1197178
Created September 6, 2011 10:06
SyntaxHighlighter-script Render Fallback
//L.Sauer 2011; Public Domain
//renders code even without SyntaxHighlighter present
//see http://xhr2.blogspot.com/2011/09/alexgorbatchevcom-down-changing.html
//dp is the typical SyntaxHighlighter object;
//use the class 'shFallback' for styling the fallback <pre>-tag
if(typeof dp === 'undefined' ){
(function(){
var els = document.getElementsByTagName('script');
var elClass = null; //or e.g. class 'shFallback'
for(var i in els) {
@lsauer
lsauer / gist:1197340
Created September 6, 2011 11:45
ANSI C Determine the sign of a variable
( a >= 0 && ~a >= 0 )
First statement ensures that the variable is positive, the second ensures that no sign-bit is flipped.
@lsauer
lsauer / gist:1208350
Created September 10, 2011 14:16
JavaScript: Simple multi-timer for logging performance of an html page
function j2timer(e){
window.tdiff || (window.tdiff = []);
window.tdlog || (window.tdlog = []);
window.fnred || (window.fnred = function(a,b){return a-b;});
//toogle statement
if( (tdiff.length+1) % 2){
tdiff.push( Date.now());
return;
}
@lsauer
lsauer / gist:1209992
Created September 11, 2011 19:18
OnBlur will not fire when changing onFocus to onKeyUp, onKeyPress, onKeyDown,...
The following code will do just what you would expect (a css rule for the element's id is applied).
<input id="searchCmpnd" onClick="this._originalWidth = this.style.width; this.style.width='50%';" onBlur="this.style.width = this._originalWidth;" />
However when the onClick is changed to onKeyUp, onKeyPress, onKeyDown it will expand but not retract. This is because the element is not marked focused, or because the event order got messed up (in Chrome >12).
Include this.focus();
<input id="searchCmpnd" onClick="this._originalWidth = this.style.width; this.focus(); this.style.width='50%';" onBlur="this.style.width = this._originalWidth;" />
@lsauer
lsauer / gist:1210023
Created September 11, 2011 19:45 — forked from timbroder/gist:1209944
insert update url rewrite
#INSERT INTO `core_url_rewrite` (`store_id`, `category_id`, `product_id`, `id_path`, `request_path`, `target_path`, `is_system`)
SELECT 1 AS `store_id`,
`sub`.`category_id`,
`sub`.`entity_id` AS `product_id`,
CONCAT('product', '/', `entity_id`, '/', `category_id`) AS `id_path`,
`value` AS `request_path`,
CONCAT('catalog/product/view/id/', `entity_id`, '/category/', `category_id`) AS `target_path`,
1 AS `is_system`
FROM
(SELECT
@lsauer
lsauer / gist:1213403
Created September 13, 2011 08:20
Submitting a browser-like HTTP request
#L sauer 2011, public domain
def main():
import urllib2
furl = urllib2.Request('http://www.mypacm.us.gov/bin/www_bget?one=two')
furl.add_header('Referer', 'http://www.mypacm.us.gov/')
furl.add_header('User-agent', 'Mozilla/5.0 AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.35 Safari/535.1')
furl.add_header('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
furl.add_header('Accept-Encoding', 'gzip,deflate,sdch')
furl.add_header('Accept-Language', 'en-US,en;q=0.8')
#furl.add_header('Origin', 'http://www.mypacm.us.gov/bin/')
@lsauer
lsauer / gist:1221325
Created September 16, 2011 06:09
Javascript word frequency counter - word histogramm
//l.sauer 2011, public domain
//returns a hash table with the word as index and frequency as value; good for svg / canvas -plotting or other experiments
//[:punct:] Punctuation symbols . , " ' ? ! ; : # $ % & ( ) * + - / < > = @ [ ] \ ^ _ { } | ~
var wordcnt = function(id){
var hist = {}, words = document.getElementById(id).innerText.split(/[\s*\.*\,\;\+?\#\|:\-\/\\\[\]\(\)\{\}$%&0-9*]/)
for( i in words)
if(words[i].length >1 )
hist[words[i]] ? hist[words[i]]+=1 : hist[words[i]]=1;
return hist;
};
@lsauer
lsauer / gist:1238036
Created September 23, 2011 18:04
Social Box for websites with Facebook, Twitter and Google plus one - HTML 4 / 5
<style type="text/css">
.socialBoxV, .socialBoxH { {
float: right;
margin: 15px 20px 0 0;
padding: 7px 7px 7px 14px;
background-color: #FCF6C4;
border-radius: 5px;
-o-transition: all 0.7s linear;
-moz-transition: all 0.7s linear;
@lsauer
lsauer / gist:1238371
Created September 23, 2011 20:26
Color and grey palette in PHP and Javascript
//author: unknown/ extended by Lo Sauer (2000); Codebase yr2000
//descr: Color and grey palette in PHP with Javascript interaction
<script language="JavaScript">
var coRed=127;coGreen=127;coBlue=255; // Original color
var Hex=new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
function rgb2hex(cRed,cGreen,cBlue){
cColor=Hex[Math.floor(cRed/16)]+Hex[cRed%16]+Hex[Math.floor(cGreen/16)]+Hex[cGreen%16]+Hex[Math.floor(cBlue/16)]+Hex[cBlue%16];