Skip to content

Instantly share code, notes, and snippets.

View bxt's full-sized avatar

Bernhard Häussner bxt

View GitHub Profile
@bxt
bxt / trashable-jQuery.js
Created February 26, 2011 15:45
jQuery to set href arrtibute of the 5th link to "#"
var _=["a","eq","href","attr","#",4];
$(_[0])[_[1]](_[5])[_[3]](_[2],_[_[5]]);
@bxt
bxt / public-dir.sh
Created February 26, 2011 15:47
Command to share a folder "/srv/share" with the group "sharer"
chgrp -R sharer /srv/share; find /srv/share/* -type d -exec chmod g+srw {} \;
@bxt
bxt / gist:845320
Created February 26, 2011 15:49
Get rid of PHP short tags with these regexp
replace "<\?=\$(.*)\?>" to "<?php echo \$\1; ?>"
and then "<\?(?!php)" to "<?php"
@bxt
bxt / Fail-array.access.php
Created February 26, 2011 15:01
PHP is failing to immidately acces an array returned by a function
<?php
echo foo()[0]; // PHP Parse error: syntax error, unexpected '[', expecting ',' or ';'
function foo() {
return array("cant thouch this");
}
// use this instead:
list($tmp)= foo();
echo $tmp;
@bxt
bxt / ternary-varname.php
Created February 26, 2011 15:22
Accessing a var by ternary operator condition in PHP
<?php $var="foo"; $foo=array(); ${true?$var:"bar"}[]=$var; var_dump($foo);
/* Output:
array(1) {
[0]=>
string(3) "foo"
}
*/
@bxt
bxt / whats-happening.php
Created February 26, 2011 15:26
Some really cryptic php, helps to test your operator precedence knowledge
<?php echo 0 || 1337 ? 4 : 0 | -4 ? 5 : 0 ? 7 ^ 5: 0 ; // 2
@bxt
bxt / really-annoying.sh
Created February 26, 2011 15:40
if you really want to annoy someone (or the whole office) run this on their terminal:
(while true; do echo -n $'\a';sleep 1; done;) &
@bxt
bxt / this-in-setTimeout-call.js
Created February 26, 2011 15:56
When using setTimeout inside Objects, making this availible in callback
setTimeout(function (obj){(function(){
/*here goes the callback code*/
}).apply(obj)},time,this);
@bxt
bxt / Avoid hundrets isset() calls.php
Created February 26, 2011 14:42
Prepare array with false-Values for undefinded keys with array_fill_keys (in PHP >= 5.3.0)
<?php
$in_array=array("foo"=>"fooval","baz"=>"bazval");
$in_array+=array_fill_keys(array("foo","bar","baz"),false);
// array_fill_keys is since PHP 5.2.0
// useage:
echo $in_array["foo"]?:"foodef";
// short ternary operator is since PHP 5.3.0
echo "\n";
@bxt
bxt / one-line_object_creation.php
Created February 26, 2011 15:14
How to (and not to) use & create a PHP instance in the same line.
<?php
// echo (new Foo())->$bar; // fails
// PHP Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';'
echo Foo::getI()->bar; //works
class Foo {
public function __construct() {
$this->bar="hello world\n";
}
static function getI() {