get_microtime()    :20251031100447244459
get_guid()         :_a218339a05060c75876ac534dc67891b
vardump('toto')  :
-1       => 
0        => 0
1        => 1 seconde
59       => 59 secondes
60       => 1 minute
61       => 1 minute 1 seconde
3599     => 59 minutes 59 secondes
3600     => 1 heure
3601     => 1 heure 1 seconde
3660     => 1 heure 1 minute
3661     => 1 heure 1 minute 1 seconde
7202     => 2 heures 2 secondes
7320     => 2 heures 2 minutes
7322     => 2 heures 2 minutes 2 secondes
10799    => 2 heures 59 minutes 59 secondes
86400    => 1 jour
356521   => 4 jours 3 heures 2 minutes 1 seconde
<?php
/**
 * @page bibliotheque.php
 * @brief Cette page contient quelques fonctions à mettre de côté.
 *
 * @author hughes monget
 * @see http://monget.com/
 */
/**
 * Générateur de nombres aléatoires utilisant l'algorithme SHA1 sur lui-même.
 *
 * @param $int_minimum - integer - intevale
 * @param $int_maximum - integer - intevale
 * @return int - retourne un entier aléatoire dans l'intervale demandé ou un int sinon.
 */
function sha_rand($int_minimum = NULL, $int_maximum = NULL)
{
    // On génère une seed.
    static $arr_raw_nombre, $int_index;
    if (!$arr_raw_nombre)
    {
        $arr_raw_nombre = sha1(uniqid(mt_rand(), TRUE).microtime().mt_rand(), TRUE);
        $int_index      = 0;
    }
    // On regénère le hash que l'on applique à lui-même.
    if ($int_index == 5)
    {
        // Le sha1 renvoie 160 bits soit 5 mots de 32 bits.
        $arr_raw_nombre  = sha1($arr_raw_nombre, TRUE);
        $int_index       = 0;
    }
    // On extrait une valeur (aléatoire) du hash.
    $int_valeur_aleatoire = substr($arr_raw_nombre, 4 * $int_index, 4);
    $int_valeur_aleatoire = unpack('L', $int_valeur_aleatoire);
    $int_valeur_aleatoire = reset($int_valeur_aleatoire);
    $int_index++;
    // On gère l'intervale demandé.
    if (!is_null($int_minimum) && !is_null($int_maximum))
    {
        if ($int_minimum <= $int_maximum)
        {
            $int_valeur_aleatoire = $int_minimum + abs($int_valeur_aleatoire) % abs($int_maximum - $int_minimum + 1);
        }
    }
    return $int_valeur_aleatoire;
}
/** @return string - Retourne une chaîne composée de chiffres correspondant au temps courant . */
function get_microtime()
{
    static $int_precision = 6;
    $flo_stamp = microtime(TRUE);
    return (date('YmdHis', $flo_stamp).substr(number_format($flo_stamp, $int_precision), -$int_precision));
}
/** @return string - Retourne un pseudo Globally Unique Identifier */
function get_guid()
{
    return ('_'.md5(uniqid(mt_rand(), TRUE)));
}
/** Dump HTML de la variable passée en paramètre. */
function vardump($mixed)
{
    ob_start(); var_dump($mixed); $var_dump = trim(ob_get_clean());
    echo '<div style="display:block;visibility:visible">';
    echo '<pre style="background:#ddd;display:inline;border:1px solid red;">';
    echo htmlspecialchars($var_dump, ENT_QUOTES);
    //echo htmlspecialchars(var_export($mixed, TRUE), ENT_QUOTES);
    echo '</pre>';
    echo '</div>';
}
/** Anti register_globlal */
foreach (array_keys($_REQUEST) as $str_varname)
{
    ${$str_varname} = NULL;
    unset(${$str_varname});
}
/** Désactivation du magic_quotes */
function recursive_stripslashes($mixed)
{
    if (is_array($mixed))
    {
        return array_map(__FUNCTION__, $mixed);
    }
    else
    {
        return stripslashes($mixed);
    }
}
function desactiver_magic_quotes()
{
    static $bool_deja_appele = FALSE;
    if (!$bool_deja_appele)
    {
        if (get_magic_quotes_gpc())
        {
            $arr_arr_global = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
            foreach ($arr_arr_global as &$arr_global)
            {
                $arr_global = array_map('recursive_stripslashes', $arr_global);
            }
        }
        $bool_deja_appele = TRUE;
    }
}
desactiver_magic_quotes();
/** Anti-appel via HTTP - CLI uniquement - décommenter le exit */
$arr_str_variable_serveur = array('REQUEST_METHOD', 'DOCUMENT_ROOT', 'SERVER_NAME', 'SERVER_PROTOCOL');
foreach ($arr_str_variable_serveur as $str_variable_serveur)
{
    if (!empty($_SERVER[$str_variable_serveur]))
    {
        //exit('CLI only !');
    }
}
/**
 * Fonction de conversion d'une durée en secondes en heures, minutes et secondes.
 *
 * @param $int_duree_seconde - integer - Une durée en secondes.
 * @return string - "6 heures 4 minutes 21 secondes"
 */
function convertir_seconde($int_duree_seconde)
{
    $str_conversion = '';
    if (is_int($int_duree_seconde) && $int_duree_seconde >= 0)
    {
        if ($int_duree_seconde == 0)
        {
            $str_conversion = '0';
        }
        else
        {
            $str_separateur = '';
            foreach (array('jour' => 86400, 'heure' => '3600', 'minute' => '60', 'seconde' => '1') as $str_unite => $int_nombre_seconde)
            {
                $int_nombre_unite = intval($int_duree_seconde / $int_nombre_seconde);
                if ($int_nombre_unite > 0)
                {
                    $int_duree_seconde = $int_duree_seconde % $int_nombre_seconde;
                    $str_conversion .= $str_separateur.$int_nombre_unite.' '.$str_unite.(($int_nombre_unite > 1)?'s':'');
                    $str_separateur = ' ';
                }
            }
        }
    }
    return $str_conversion;
}
/**
 * Fonction de suppression des balises html.
 *
 * @note on ne remplace que si nécessaire. (plus performant sur de grandes chaînes)
 * @note on tant qu'on trouve, on remplace. (éviter les attaques de types <scr<script>ipt> )
 * @param $s - string
 * @return string
 */
function supprimer_balise($s)
{
    // Supprimer des motifs spécifiques et leur *contenu*.
    // Balises PHP, Commentaires HTML.
    foreach (array('|<\?.*\?>|sU', '/<!--.*-->/Us') as $str_pattern)
    {
        while (preg_match($str_pattern, $s))
        {
            $s = preg_replace($str_pattern, '', $s);
        }
    }
    // Supprimer spécifiquement des tags et leur *contenu*.
    foreach (array('script', 'head') as $str_tag)
    {
        $str_pattern = '|<'.$str_tag.'.*</'.$str_tag.'>|sUi';
        while (preg_match($str_pattern, $s))
        {
            $s = preg_replace($str_pattern, '', $s);
        }
    }
    // Supprimer tous les tags eux-même et uniquement eux.
    // Matche <toto> , <toto tutu="tata">, </toto>, <toto />
    $str_pattern = '/<[^>]+>/U';
    while (preg_match($str_pattern, $s))
    {
        $s = preg_replace($str_pattern, '', $s);
    }
    $s = html_entity_decode($s);
    return $s;
}
/**
 * Fonction de suppression des lignes vides.
 *
 * @param $s - string
 * @return string
 */
function supprimer_ligne_vide($s)
{
    // Uniformiser les fins de lignes.
    $s = str_replace("\r\n", "\n", $s); // dos2unix
    $s = str_replace("\r",   "\n", $s); // mac2unix
    // Eclater en tableau.
    $arr = explode("\n", $s);
    // Supprimer les lignes vides.
    foreach ($arr as $k => $tmp)
    {
        $tmp = str_replace(chr(160), ' ', $tmp);
        if (!trim($tmp))
        {
            unset($arr[$k]);
        }
    }
    return implode("\r\n", $arr);
}
//--------------------------------------------------------------------------
/**
 * Fonction de conversion des lettres accentuées d'une chaîne en
 * leur équivalent non accentué.
 *
 * @param $str - string - La chaîne à traiter
 * @return string - La chaîne sans accent.
 */
function convertir_accent($str)
{
    static $acc_o = "àÀáÁâÂäÄãÃåÅèÈéÉêÊëËìÌíÍîÎïÏòÒóÓôÔöÖøØùÙúÚûÛüÜÿŸñÑçÇ";
    static $acc_r = "aAaAaAaAaAaAeEeEeEeEiIiIiIiIoOoOoOoOoOuUuUuUuUyYnNcC";
    return strtr($str, $acc_o, $acc_r);
}
//--------------------------------------------------------------------------
echo '<pre>';
echo 'get_microtime()    :',get_microtime(),'<br />';
echo 'get_guid()         :',get_guid(),'<br />';
echo 'vardump(\'toto\')  :',vardump('toto'),'<br />';
echo '</pre>';
echo '<pre>';
foreach (array(-1, 0, 1, 59, 60, 61, 3599, 3600, 3601, 3660, 3661, 7202, 7320, 7322, 10799, 86400, 356521) as $int_seconde)
{
    echo str_pad($int_seconde, 8, ' ', STR_PAD_RIGHT),' => ',convertir_seconde($int_seconde),"\n";
}
echo '</pre>';
highlight_file(__FILE__);