Tag Archive for function

Convert Database/Variable Name to Human Title

<?
class inflector extends inflector_Core {
public static function titlize($rawTitle) {
if(strlen($rawTitle) < 3) {
return strtoupper($rawTitle);
} else {
return ucwords(self::humanize($rawTitle));
}
}
}

echo inflector::titlize("news_items"); // News Items
echo inflector::titlize("id"); // ID
?>

source

Function Timing Example (includes jQuery)

$(function(){
console.info("Start Test");
var d = new Date();//Get our start time
console.info(d.getSeconds() + " " + d.getMilliseconds());

//Run our test
var testBody = "";
for (var i=0; i<1000; i++){
testBody += "<div class='testable"+i+"'>";
}
$("body").append(testBody);
for (var j=0; j<1000; j++){
$(".testable"+j);
}

var d = new Date();//Get our end time
console.info(d.getSeconds() + " " + d.getMilliseconds());
console.info("End Test");
/**
* Console will now log 2 times, the difference between
* them is how long the test took to run
*/
});

source

mysql query connect function

function query($sql){
$dbhost = "localhost";
$dbuname = "root";
$dbpass = "root";
$dbname = "disco_conv";
$conxion = mysql_connect($dbhost, $dbuname, $dbpass);
$result=mysql_db_query($dbname,$sql,$conxion);
return $result;
}

source

List JQuery Functions with JQuery & Firebug

jQuery.fn.each(console.log(i));

source

Custom PHP Error Handler

function logErrorsErrorHandler($errno, $errstr, $errfile, $errline) {
switch ($errno) {
case E_USER_ERROR:
error_log(
"<b>My ERROR</b> [$errno] $errstr<br />
".
"  Fatal error on line $errline in file $errfile".
", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />
".
"Aborting...<br />
"
);
exit(1);
break;

case E_WARNING:
case E_USER_WARNING:
error_log("<b>My WARNING</b> [$errno] $errstr<br />
");
break;

// case E_DEPRECATED:
// case E_USER_DEPRECATED:
case E_STRICT:
break;

case E_USER_NOTICE:
error_log("<b>My NOTICE</b> [$errno] $errstr<br />
");
break;

default:
error_log("Unknown error type: [$errfile:$errline] [$errno] $errstr<br />
");
break;
}

/* Don't execute PHP internal error handler */
return true;
}

ini_set('error_log', 'error_log');
ini_set('log_errors', 'On');

set_error_handler("logErrorsErrorHandler");

source

When to use the prototype of a Function

// consider:
function Grape(type) {
this.type = type || 'Xynomavro';
}

Grape.prototype.squash = function () {
return this.type.substring(0, 3);
};

var grape = new Grape();

alert(grape.squash());

// versus:
var grapes = {
barrel : [],
squash : function (grape) {
return grape.substring(0, 3);
}
};

grapes.barrel.push('Nebbiolo');

alert(grapes.squash(grapes.barrel[0]));

source

AS3 Calling a parent function from a child movieclip

// adjust "this.parent.parent" as needed

if(this.parent.parent != null){
var parentObj:Object = this.parent.parent as Object;
parentObj.traceMe()
}

// in your parent SWF you will want, of course, a function like this:
// function traceMe():void
// {
//      trace("success");
// }

source

add up any number of parameters

function sum() {
var res = 0;
for (var i=0; i < arguments.length; i++) {
res += arguments[i];
}
return res;
}

source

Auto-highlight Cheatsheeter

function report($text = '', $ret = true, $_re = true)
{
$out = array();

preg_match_all('/functions+(&?[a-z]w+)s*(s*([^{]*)s*)/is', $text, $test);
arsort($test[1]);

foreach ($test[1] as $key => $val)
{ // evitamos..
if (preg_match('/^[A-Z_]/', $val) && $_re)
{
continue;
}

// simple...
$val = htmlspecialchars($val);
$args = htmlspecialchars($test[2][$key]);

$args = preg_replace('/('|"|&quot;?)(.*?)1/', '<span style="color:green;white-space:nowrap"></span>', $args); // "string's"
$args = preg_replace('/(true|false|null)/i', '<span style="color:purple;font-weight:bold"></span>', $args); // bool's
$args = preg_replace('/[0-9]+/', '<span style="color:black;font-weight:bold"></span>', $args); // INTs
$args = preg_replace('/$[a-z0-9_]+/i', '<span style="color:darkblue"></span>',$args); // $vars
$args = preg_replace('/[a-z0-9_]+(.*?)/i', '<span style="color:gray"></span>', $args); // array()

$args = trim($args)? $args: $args.'<span style="color:black;font-weight:bold;font-style:italic">void</span>';
$args = preg_replace('/s+=s+/', '&nbsp;=&nbsp;', $args);
$args = preg_replace('/s*,s+/', ',&nbsp;', $args);

$out []= "	<span style="color:gray">
<span style="color:red"><em>$val</em></span><strong>(</strong>$args<strong>)</strong>;
</span>";
}

$out = join("
", $out);

if ( ! $ret)
{
echo $ret;
}
return $out;
}

source

Check functions jQuery extension

jQuery.extend(jQuery, {
checkFunction: function(fPointer, lastFpPointer){
var newFunction = false;
if(jQuery.isFunction(fPointer)){
newFunction = fPointer;
}else if (/(.*)/igm.test(fPointer)){
newFunction = new Function(fPointer);
}else{
try{
var mayBeFunction = (eval(fPointer));
if(jQuery.isFunction(mayBeFunction)){
newFunction = mayBeFunction;
}else{
newFunction = lastFpPointer;
}
}catch(e){
newFunction =lastFpPointer;
}
}
return newFunction;
});

source