You can find here the code for some general purpose functions. |
|
PHP | |
|
Remove String Portions |
|
Remove portions of text from a string, given the two delimiters |
|
Feb 28th 2012 |
|
function remove_string_portion( $string,
$start_word, $end_word,
$how_many_occurrances = 0, /* refer below */
$REPLACE_TERM, /* if you want to replace the portion with some text */
&$stripped /* output returned by reference */
)
{
/* $how_many_occurrances
0 : ALL
+n : up to the n occurrances
-n : only the nth occurrance
*/
$CNT = 0 ;
/* check if both delimiters are consistent */
if ( strlen( $start_word ) > 0 && strlen( $end_word ) > 0 )
{
while( true )
{
$startpos = stripos($string,$start_word,0) ;
$endpos = ( $startpos !== false ) ? stripos($string,$end_word,$startpos+strlen($start_word) ) : false ;
$endpos = ( $endpos !== false ) ? $endpos + strlen($end_word) : false ; // shifting delimiter to the far end
/* if one of such conditions are met, return operations counter */
if ( ( $startpos === false || $endpos === false ) ) return $CNT ; // no both portions delimiters are found
else if ( $how_many_occurrances < 0 && $CNT < abs( $how_many_occurrances ) ) return $CNT ;
else if ( $how_many_occurrances > 0 && $CNT == abs( $how_many_occurrances ) ) return $CNT ;
$BEFORE = substr( $string, 0, $startpos );
$AFTER = substr( $string, $endpos, strlen( $string ) - 1 );
$stripped = "$BEFORE$REPLACE_TERM$AFTER" ;
$CNT++ ;
$string = $stripped ;
}
}
return $CNT ;
}
$LEFT_DELIMITER = "test" ;
$RIGHT_DELIMITER = "stupid" ;
$IN = "This is just a test, a stupid test where we are stripping portions from a text" ;
$bSTRIPPED = remove_string_portion( $IN, $LEFT_DELIMITER, $RIGHT_DELIMITER, 0, "", $OUT );
echo "LEFT DELIMITER: $LEFT_DELIMITER
RIGHT DELIMITER: $RIGHT_DELIMITER
IN STRING: $IN
OUT STRING: $OUT " ; |
|
If you want to improve this function, just send me your code and you'll be listed here. |
|
|
|
Unlink recursive |
|
Delete recursively all elements (files and subfolders) in a folder. |
|
Aug 8th 2011 |
|
function unlinkRecursive( $dir, $deleteRootToo, &$GLOBALFILESCNT )
{
if(!$dh = @opendir($dir))
{
return false ;
}
$FILESCNT = 0 ;
while (false !== ($obj = readdir($dh)))
{
if($obj == '.' || $obj == '..')
{
continue;
}
if ( !is_dir( $dir . '/' . $obj ) )
{
$FILESCNT++ ;
$GLOBALFILESCNT++ ;
}
if ( !@unlink( $dir . '/' . $obj ) )
{
unlinkRecursive($dir.'/'.$obj, $deleteRootToo, $GLOBALFILESCNT );
}
}
closedir($dh);
if ( $deleteRootToo ) $bREMOVED = @rmdir($dir);
return true;
} |
|
If you want to improve this function, just send me your code and you'll be listed here. |
|
|
|
Makedir Recursive |
|
Creates multi-level folders |
|
Aug 8th 2011 |
|
function mkdir_recursive($pathname, $mode)
{
is_dir(dirname($pathname)) || mkdir_recursive(dirname($pathname), $mode);
return is_dir($pathname) || @mkdir($pathname, $mode);
} |
|
If you want to improve this function, just send me your code and you'll be listed here. |
|
|
|
Load source code |
|
load source code (js or php) recursively from a multi-level folder, so that you'll just have to upload your components to the server, call this function from your page and never worry about <SCRIPT> or require_once |
|
Aug 9th 2011 |
|
function load_src_code_dir( $PATHtoSOURCEcode, $EXTENSIONS, &$ERRCODE )
{
$EXTENSIONSarray = ( strpos( $EXTENSIONS, "|", 0 ) !== false ) ? explode( "|", $EXTENSIONS ) : array( "$EXTENSIONS" );
// quick path checking ...
$PATHtoSOURCEcode = trim( $PATHtoSOURCEcode );
if ( substr( $PATHtoSOURCEcode, -1 ) != "/" ) $PATHtoSOURCEcode .= "/" ;
if ( file_exists( $PATHtoSOURCEcode ) )
{
$FILESarray = scandir( $PATHtoSOURCEcode );
$N = count( $FILESarray );
if ( $N == 0 )
{
$ERRCODE = 1 ;
}
else
{
foreach( $FILESarray AS $KEY => $FILE )
{
if ( strcmp( $FILE, "." ) != 0 && strcmp( $FILE, ".." ) != 0 )
{
if ( is_file( "$PATHtoSOURCEcode$FILE" ) )
{
$CANDIDATEextension = pathinfo( $FILE, PATHINFO_EXTENSION ) ;
if ( in_array( "$CANDIDATEextension", $EXTENSIONSarray ) )
{
switch( "$CANDIDATEextension" )
{
case "js":
echo "<SCRIPT LANGUAGE=\"javascript\" TYPE=\"text/javascript\" SRC=\"$PATHtoSOURCEcode$FILE\"></SCRIPT>\r\n" ;
break ;
case "php":
{
require_once( "$PATHtoSOURCEcode$FILE" ) ;
}
break ;
}
}
}
else if ( is_dir( "$PATHtoSOURCEcode$FILE" ) )
{
load_src_code_dir( "$PATHtoSOURCEcode$FILE", $EXTENSIONS, $ERRCODE );
}
}
}
}
}
else $ERRCODE = 1 ;
} |
|
If you want to improve this function, just send me your code and you'll be listed here. |
|
|
|
Get string between |
|
Given the input string, it retrieves all chars between two given substring |
|
Aug 8th 2011 |
|
function get_string_between($string, $start, $end, $startpos, &$endpos )
{
$string = " ".$string ;
$ini = stripos($string,$start, $startpos) ;
if ($ini == 0) return "" ;
$ini += strlen($start) ;
$endpos = stripos($string,$end,$ini) ;
$endpos += strlen($end) ;
$len = $endpos - $ini - strlen($end) ;
return substr($string,$ini,$len);
} |
|
If you want to improve this function, just send me your code and you'll be listed here. |
|
|
|
Flush into File |
|
Flush the input string content into a file of given name |
|
Aug 8th 2011 |
|
function flushINTOfile( $filename, $contents )
{
if ( ( $HFILE = fopen( "$filename", "w+" ) ) === false ) return false ;
fwrite( $HFILE, $contents );
$bCLOSED = fclose( $HFILE );
return $bCLOSED ;
} |
|
If you want to improve this function, just send me your code and you'll be listed here. |
|
|
|
Is empty |
|
check whether a variable is empty or not |
|
Aug 9th 2011 |
|
function is_empty( $in ) { return ( !isset( $in ) || strlen( $in ) == 0 ) ? true : false ; } |
|
If you want to improve this function, just send me your code and you'll be listed here. |
|
|
|
Age |
|
Computes the age, given the birth and today date |
|
Aug 8th 2011 |
|
function age( $BIRTH_DATE, $TODAY_DATE ) // dates in YYYY-MM-DD
{
$AGE = 0 ;
if ( strpos( $BIRTH_DATE, "-", 0 ) === false || strpos( $TODAY_DATE, "-", 0 ) === false ) return -1 ;
else
{
$BIRTHDATEarray = explode( "-", $BIRTH_DATE ); $TODAYarray = explode( "-", $TODAY_DATE );
$BIRTH_YYYY = intval( $BIRTHDATEarray[0] );
$BIRTH_MM = intval( $BIRTHDATEarray[1] );
$BIRTH_DD = intval( $BIRTHDATEarray[2] );
$TODAY_YYYY = intval( $TODAYarray[0] );
$TODAY_MM = intval( $TODAYarray[1] );
$TODAY_DD = intval( $TODAYarray[2] );
$AGE = $TODAY_YYYY - $BIRTH_YYYY ;
if ( $AGE < 0 ) return -1 ;
else if ( $AGE == 0 ) return 0 ;
else
{
if ( $TODAY_MM < $BIRTH_MM ) $AGE-- ;
else if ( $TODAY_MM == $BIRTH_MM )
{
if ( $TODAY_DD < $BIRTH_DD ) $AGE-- ;
}
}
}
return $AGE ;
} |
|
If you want to improve this function, just send me your code and you'll be listed here. |
|
|
|
Strip double spaces |
|
Delete all double spaces in an input string |
|
Aug 8th 2011 |
|
function strip_doublespaces( $str )
{
$pat[0] = "/^\s+/" ; $pat[1] = "/\s{2,}/" ; $pat[2] = "/\s+\$/" ;
$rep[0] = "" ; $rep[1] = " " ; $rep[2] = "" ;
$str = preg_replace( $pat, $rep, $str ) ;
return $str ;
} |
|
If you want to improve this function, just send me your code and you'll be listed here. |
|
|
|
|
JS | |
|
Check date |
|
Check if input date is correct |
|
Aug 8th 2011 |
|
function checkdate(m, d, y) { return m > 0 && m < 13 && y > 0 && y < 32768 && d > 0 && d <= ( new Date(y, m, 0 ) ).getDate() ; } |
|
If you want to improve this function, just send me your code and you'll be listed here. |
|
|
|
Function caller |
|
It retrieves which function called the running one |
|
Aug 8th 2011 |
|
function get_fns_caller() { return arguments.callee.caller.toString() ; } |
|
If you want to improve this function, just send me your code and you'll be listed here. |
|
|
|
Check control data |
|
Given a control ID (TD,INPUT,TEXTAREA,SELECT), check the contents via a regular expression |
|
Aug 8th 2011 |
|
function check_ctrl_data( ctrlid, regEXPR )
{
var bOK = false ;
var CTRL = document.getElementById( ctrlid );
if ( CTRL != null )
{
switch ( CTRL.tagName.toLowerCase() )
{
case "input" :
var INPUTvalue = CTRL.value ;
RegExpOBJ = new RegExp( regEXPR ) ;
bOK = this.responseValue = RegExpOBJ.test( INPUTvalue );
break;
case "textarea" :
var INPUTvalue = CTRL.value ;
RegExpOBJ = new RegExp( regEXPR ) ;
bOK = this.responseValue = RegExpOBJ.test( INPUTvalue );
break;
case "select" :
var INPUTvalue = CTRL.options[ CTRL.selectedIndex ] ;
RegExpOBJ = new RegExp( regEXPR ) ;
bOK = this.responseValue = RegExpOBJ.test( INPUTvalue );
break;
case "td" :
var INPUTvalue = CTRL.innerHTML ;
RegExpOBJ = new RegExp( regEXPR ) ;
bOK = this.responseValue = RegExpOBJ.test( INPUTvalue );
break;
}
}
return bOK ;
} |
|
If you want to improve this function, just send me your code and you'll be listed here. |
|
|
|
|