<?
/**
* @return string
* @param string $haystack Text To Parse
* @param string $bfstarttext start string
* @param string $endsection end string
* @desc return the string included between $bfstarttext and $endsection
*/
function stripfromtext($haystack, $bfstarttext, $endsection) {
$startpostext = $bfstarttext;
$startposlen = strlen($startpostext);
$startpos = strpos($haystack, $startpostext);
$endpostext = $endsection;
$endposlen = strlen($endpostext);
$endpos = strpos($haystack, $endpostext, $startpos);
return substr($haystack, $startpos + $startposlen, $endpos - ($startpos + $startposlen));
}
/**
* @return array
* @param string $haystack Text To Parse
* @param string $bfstarttext start string
* @param string $endsection end string
* @desc return an array of the strings included between $bfstarttext and $endsection
*/
function &stripfromtextarray($haystack, $bfstarttext, $endsection, $myarray=array(), $offset=0) {
$startpostext = $bfstarttext;
$startposlen = strlen($startpostext);
$startpos = strpos($haystack, $startpostext, $offset);
$endpostext = $endsection;
$endposlen = strlen($endpostext);
$endpos = strpos($haystack, $endpostext, $startpos);
$myarray[] = substr($haystack, $startpos + $startposlen, $endpos - ($startpos + $startposlen));
$offset = $endpos;
if (is_numeric(strpos($haystack, $startpostext, $offset))) {
return stripfromtextarray($haystack,$startpostext, $endpostext, &$myarray, $offset);
}else{
return $myarray;
}
}
/**
* @return string
* @param int $len lunghezza
* @desc return a string random
*/
function random_string($min=10,$max=25){
$pass = NULL; $lchar=NULL;
$len = rand($min,$max);
for($i=0; $i<$len; $i++) {
$char = chr(rand(48,122));
while (!ereg("[a-zA-Z0-9]", $char)){
if($char == $lchar)
continue;
$char = chr(rand(48,90));
}
$pass .= $char;
$lchar = $char;
}
return $pass;
}
/**
* @return int
* @param int $len lunghezza
* @desc return an int random
*/
function random_int($min=10,$max=25){
$randnum = "";
$len = rand($min,$max);
for ($i=0;$i<$len;$i++){
$randnum .= rand(0, 9);
}
return $randnum;
}
/**
* @return string
* @param string $str Text To Capitalize
* @param int $index first word to capitalize
* @desc Capitalze a string
*/
function capitalize($str,$index=0){
$arrstr = explode(" ",$str);
$begin = substr($arrstr[$index],0,1);
$end = substr($arrstr[$index],1,strlen($arrstr[$index]));
$arrstr[$index] = strtoupper($begin).$end;
return implode(" ",$arrstr);
}
function do_bytes($size) {
$kb = 1024; // Kilobyte
$mb = 1024 * $kb; // Megabyte
$gb = 1024 * $mb; // Gigabyte
$tb = 1024 * $gb; // Terabyte
if($size==0) $size = "0.1";
if($size < $kb) {
$s = $size; $um = "b";
} else if($size < $mb) {
$s = round($size/$kb,2); $um = "Kb";
} else if($size < $gb) {
$s = round($size/$mb,2); $um = "Mb";
} else if($size < $tb) {
$s = round($size/$gb,2); $um = "Gb";
} else {
$s = round($size/$tb,2); $um = "Tb";
}
return array("size"=>$s, "um"=>$um);
}
/**
* @return array
* @param $arr array
* @param $value string
* @param $bPreserveKeys bool
* @desc Remove an element from the array preserving key=>val association if not different specified
*/
function array_remove($arr,$value,$bPreserveKeys=true){
$ret = array(); $y = 0;
foreach($arr as $k=>$val){
if($val!=$value){
if($bPreserveKeys) $ret[$k] = $val;
else $ret[] = $val;
}
}
return $ret;
}
function getHexColor($color){
$return = array();
for($i=0; $i<count($color); $i++){
$return[] = sprintf("%02s",dechex(floor($color[$i] * 255)));
}
return $return;
}
/**
* @return string parsed
* @param string $path path/to/data like XPath
* @param string $string XML string
* @param string $separator path-separator(/)
*/
function parsexml($path,$string,$separator="/"){
$levels = explode($separator,$path);
if(count($levels)>0){
$node = $levels[0];
$xml = stripfromtext($string,"<".$node.">","</".$node.">");
if(count($levels)==1)
return $xml;
else {
array_shift($levels);
return parsexml(implode("/",$levels),$xml,$separator);
}
}
}
/**
* @return first position of the last coccurence of a string
* @param string $haystack
* @param string $needle string to search
* @param string $offset
*/
function my_strrpos($haystack, $needle, $offset=0) {
// same as strrpos, except $needle can be a string
$strrpos = false;
if (is_string($haystack) && is_string($needle) && is_numeric($offset)) {
$strlen = strlen($haystack);
$strpos = strpos(strrev(substr($haystack, $offset)), strrev($needle));
if (is_numeric($strpos)) {
$strrpos = $strlen - $strpos - strlen($needle);
}
}
return $strrpos;
}
?>