Files
2020-01-27 08:56:08 +01:00

349 lines
6.8 KiB
PHP

<?php
#[F]# X_format
# @param $val mixed Donne(s) traiter
# @param $typ string Type de traitement appliquer
# @param [$sup] mixed Parametres supplmentaires
# @return mixed Donne(s) formates
if(!function_exists('X_format')){
function X_format($val,$typ,$sup = ''){
$return = $val;
$tabjour = array('dimanche','lundi','mardi','mercredi','jeudi','vendredi','samedi');
$tabmois = array('','janvier','février','mars','avril','mai','juin','juillet','aot','septembre','octobre','novembre','dcembre');
## NEW LINES -> PARAGRAPHES
if('nl2p' === $typ && !strchr($return,'<ul>')){
$return = str_replace("\r\n",'</p><p>',$return);
$return = str_replace("\n",'</p><p>',$return);
$return = str_replace("\r",'</p><p>',$return);
$return = '<p>'.$return.'</p>';
}
## BR -> NEW LINES
elseif('br2nl' === $typ){
$return = preg_replace('/<br>/i',"\n",$return);
$return = preg_replace('/<br \/>/i',"\n",$return);
$return = preg_replace('/<br>\//i',"\n",$return);
$return = preg_replace('/<p/i',"\n".'<p',$return);
$return = str_replace("\r","\n",$return);
$return = preg_replace("/\n{2,}/","\n",$return);
}
## TELEPHONE
elseif('tel' === $typ){
$return = ereg('([[:digit:]]{2})([[:digit:]]{2})([[:digit:]]{2})([[:digit:]]{2})([[:digit:]]{2})',$return,$tab);
$return = $tab[1].'&nbsp;'.$tab[2].'&nbsp;'.$tab[3].'&nbsp;'.$tab[4].'&nbsp;'.$tab[5];
}
## VAR_DUMP AU FORMAT HTML
# @ $sup integer Gestion de le rcursivit
elseif('var_dump' === $typ){
if(!$sup) $r = '<ul><li>';
if(is_array($val)){
$r = 'array('.count($val).')<ul>';
foreach($val as $k=>$v) $r .= '<li><strong>'.$k.'</strong> : '.X_format($v,'var_dump',1);
$r .= '</ul>';
}
elseif(is_object($val)){
$r = 'object('.count(get_object_vars($val)).')<ul>';
foreach($val as $k=>$v) $r .= '<li><strong>> '.$k.'</strong> : '.X_format($v,'var_dump',1);
$r .= '</ul>';
}
else $r = gettype($val).'('.strlen($val).') "'.$val.'"</li>';
if(!$sup) $r .= '</ul>';
$return = $r;
}
## MISE EN FORME D'UNE REQUETE SQL
elseif('sql' === $typ){
$return = htmlentities($return);
$return = preg_replace('/(from|order by|limit|inner join|left join|where|having|group by)/i','<br>\\1',$return);
}
return $return;
}
}
#[F]# X_writeLogs
# @param $file string Url du fichier de logs
# @param $logs array tableau de logs
if(!function_exists('X_writeLogs')){
function X_writeLogs($file, $logs){
if(is_array($logs)) foreach($logs as $key => $log) X_writeLogs($file, $log);
else @file_put_contents(realpath(dirname(__FILE__)).'/debug/logs.txt', $logs."\n", FILE_APPEND);
}
}
#[F]# X_dateformat ex : X_dateformat("19/09/2011", "d/m/Y", "y/m/d")
# @param $date string Date au format str (selon la liste des paramètres de formatage acceptés)
# @param $inFormat string Format d'entrée (selon la liste des paramètres de formatage acceptés)
# @param $outFormat string Format de sortie
# @return string Retourne la date formatée au format souhaité
# PARAMETRES DU FORMAT D'ENTREE ACCEPTES
# d Jour du mois, sur deux chiffres (avec un zéro initial) 01 à 31
# j Jour du mois sans les zéros initiaux 1 à 31
# m Mois au format numérique, avec zéros initiaux 01 à 12
# n Mois sans les zéros initiaux 1 à 12
# Y Année sur 4 chiffres Exemples : 1999 ou 2003
# y Année sur 2 chiffres Exemples : 99 ou 03
if(!function_exists('X_dateformat')){
function X_dateformat($date, $inFormat, $outFormat){
$tab_out = array('m' => NULL, 'd' => NULL, 'y' => NULL);
$param_formats = array('d','j','m','n','y','Y');
for ($i=0; $i<strlen($inFormat);$i++)
if(!in_array($inFormat[$i], $param_formats)){
$sep = $inFormat[$i]; break;
}
$tab_inFormat = explode($sep,$inFormat);
$tab_date = explode($sep,$date);
foreach ($tab_inFormat as $key => $val){
switch ($val) {
case "d": $tab_out['d'] = $tab_date[$key]; break;
case "j": $tab_out['d'] = $tab_date[$key]; break;
case "m": $tab_out['m'] = $tab_date[$key]; break;
case "n": $tab_out['m'] = $tab_date[$key]; break;
case "Y": $tab_out['y'] = $tab_date[$key]; break;
case "y": $tab_out['y'] = $tab_date[$key]; break;
}
}
$outDate = date($outFormat, strtotime($tab_out['y'].'-'.$tab_out['m'].'-'.$tab_out['d']));
if ($outDate == False) $outDate = date($outFormat, strtotime($tab_out['y'].'\\'.$tab_out['m'].'\\'.$tab_out['d']));
$return = (!$outDate) ? false : $outDate;
return $return;
}
}
# Autres fonctions
if(!function_exists('couper_texte_html')){
function couper_texte_html($text, $length, $ending = '...', $exact = false) {
if(strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
return $text;
}
preg_match_all('/(<.+?>)?([^<>]*)/is', $text, $matches, PREG_SET_ORDER);
$total_length = 0;
$arr_elements = array();
$truncate = '';
foreach($matches as $element) {
if(!empty($element[1])) {
if(preg_match('/^<\s*.+?\/\s*>$/s', $element[1])) {
} else if(preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $element[1], $element2)) {
$pos = array_search($element2[1], $arr_elements);
if($pos !== false) {
unset($arr_elements[$pos]);
}
} else if(preg_match('/^<\s*([^\s>!]+).*?>$/s', $element[1], $element2)) {
array_unshift($arr_elements,
strtolower($element2[1]));
}
$truncate .= $element[1];
}
$content_length = strlen(preg_replace('/(&[a-z]{1,6};|&#[0-9]+;)/i', ' ', $element[2]));
if($total_length >= $length) {
break;
} elseif ($total_length+$content_length > $length) {
$left = $total_length>$length?$total_length-$length:$length-$total_length;
$entities_length = 0;
if(preg_match_all('/&[a-z]{1,6};|&#[0-9]+;/i', $element[2], $element3, PREG_OFFSET_CAPTURE)) {
foreach($element3[0] as $entity) {
if($entity[1]+1-$entities_length <= $left) {
$left--;
$entities_length += strlen($entity[0]);
} else break;
}
}
$truncate .= substr($element[2], 0, $left+$entities_length);
break;
} else {
$truncate .= $element[2];
$total_length += $content_length;
}
}
if(!$exact) {
$spacepos = strrpos($truncate, ' ');
if(isset($spacepos)) {
$truncate = substr($truncate, 0, $spacepos);
}
}
$truncate .= $ending;
foreach($arr_elements as $element) {
$truncate .= '</' . $element . '>';
}
return $truncate;
}
}
if(!function_exists('secondMinute')){
function secondMinute($seconds){
return date(($seconds < 0 ? '- ' : '')."H:i:s",(abs($seconds)-3600));
}
}
?>