Skip to content

Instantly share code, notes, and snippets.

@inferno7291
Last active February 27, 2018 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inferno7291/0148dcb1a2c09182b31087b7fce73c59 to your computer and use it in GitHub Desktop.
Save inferno7291/0148dcb1a2c09182b31087b7fce73c59 to your computer and use it in GitHub Desktop.
Function on PHP
/**
* Pasar de Kg a toneladas o al reves
*/
function formatKgtoTon($peso,$ton = false){
return $ton ? $peso*1000 : $peso/1000;
}
/**
* Formato europeo + separador decimal
*/
function formatEuDecimal($num,$decimal = 2){
return number_format($num,$decimal,',','.');
}
/**
* Limpiar los espacios dobles de un string
*/
function cleanDobleSpace($string){
return preg_replace('/\s+/', ' ',trim($string));
}
/**
* Saber si una directorio esta vacio o no
*/
function isDirEmpty($dir) {
if (!is_readable($dir)) return NULL;
$handle = opendir($dir);
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
return false;
}
}
return true;
}
/**
* Saber la url actual
*/
function getUrl($type = 'full'){
$host = $_SERVER["HTTP_HOST"];
$url = $_SERVER["REQUEST_URI"];
if($type == 'host'):
return "http://".$host;
elseif($type == 'url'):
return $url;
else:
return "http://".$host.$url;
endif;
}
/**
* Saber la ip del usuario
*/
function getRealIP() {
$ipaddress = '';
if (!empty($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(!empty($_SERVER['HTTP_X_FORWARDED']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if(!empty($_SERVER['HTTP_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if(!empty($_SERVER['HTTP_FORWARDED']))
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if(!empty($_SERVER['REMOTE_ADDR']))
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
/**
* Console LOG de javascript
* @param [type] $data [description]
* @return [type] [description]
*/
function consoleLog($data){
echo '<script>';
echo 'console.log('.json_encode($data).')';
echo '</script>';
}
/**
* Dar formato a los tamaños
*/
function formatSize($size) {
$mod = 1024;
$units = array('B','KB','MB','GB','TB','PB');
for ($i = 0; $size > $mod; $i++) {
$size /= $mod;
}
return round($size, 2).' '.$units[$i];
}
/**
* Mostrar el texto tal cual viene en el string con los saltos de linea etc..
*/
function printQuery($txt){
echo '<pre>'.$txt.'</pre>';
}
/**
* Encontrar conicidencia teniendo en cuenta el *
*/
function matchString($pattern, $str){
$pattern = preg_replace('/([^*])/e', 'preg_quote("$1", "/")', $pattern);
$pattern = str_replace('*', '.*', $pattern);
return (bool) preg_match('/^' . $pattern . '$/i', $str);
}
/**
* Generar cadena aleatoria
*/
function generatePassword($longitud = 6,$tipo = ''){
switch ($tipo) {
case 'num':
$cadena="/\D+/";
break;
case 'alf':
$cadena="/\d+/";
break;
default:
$cadena="/\W+/";
break;
}
return substr(preg_replace($cadena, "", md5(rand())).
preg_replace($cadena, "", md5(rand())) .
preg_replace($cadena, "", md5(rand())),
0, $longitud);
}
/**
* Eliminar espacios de un array
*/
function trimValue(&$value){
$value = trim($value);
}
/**
* Quitar accentos
*/
function quitarAcentos($string){
$find = "ÀÁÂÄÅàáâäÒÓÔÖòóôöÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿñÑ";
$replace = "AAAAAaaaaOOOOooooEEEEeeeeCcIIIIiiiiUUUUuuuuyñÑ";
return utf8_encode(strtr(utf8_decode($string), utf8_decode($find), $replace));
}
/**
* Preparar el string para poder ser exportado
* @param [type] &$str [description]
* @return [type] [description]
*/
function cleanDataExcel(&$str){
// escape tab characters
$str = preg_replace("/\t/", "\\t", $str);
// escape new lines
$str = preg_replace("/\r?\n/", "\\n", $str);
// convert 't' and 'f' to boolean values
/*if($str == 't') $str = 'TRUE';
if($str == 'f') $str = 'FALSE';*/
// force certain number/date formats to be imported as strings
if(preg_match("/^0/", $str) || preg_match("/^\+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str)) {
$str = "'$str";
}
// escape fields that include double quotes
if(strstr($str, '"')) $str = '"'.str_replace('"','""', $str).'"';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment