<?
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
?>
Tag Archive for function
Convert Database/Variable Name to Human Title
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
*/
});
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;
}
List JQuery Functions with JQuery & Firebug
jQuery.fn.each(console.log(i));
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");
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]));
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");
// }
add up any number of parameters
function sum() {
var res = 0;
for (var i=0; i < arguments.length; i++) {
res += arguments[i];
}
return res;
}
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('/('|"|"?)(.*?)1/', '<span style="color:green;white-space:nowrap">