<![CDATA[Teknotit Snipet Manager tarfst: Snippets: fonctions]]>http://snipet.teknotit.com/index.php <![CDATA[Détecter les requette en AJAX]]> function is_ajax_request(){ return !(empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest'); } ]]> Fri, 17 Oct 2014 06:32:25 +0200 <![CDATA[Utilisation image Gravatars]]> /****************** *@email - Email address to show gravatar for *@size - size of gravatar *@default - URL of default gravatar to use *@rating - rating of Gravatar(G, PG, R, X) */ function show_gravatar($email, $size, $default, $rating) { echo '<img src="http://www.gravatar.com/avatar.php?gravatar_id='.md5($email). '&default='.$default.'&size='.$size.'&rating='.$rating.'" width="'.$size.'px" height="'.$size.'px" />'; } ]]> Fri, 17 Oct 2014 06:29:40 +0200 <![CDATA[Force download - Forcer le download d'un fichier]]> /******************** *@file - path to file */ function force_download($file) { if ((isset($file))&&(file_exists($file))) { header("Content-length: ".filesize($file)); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . $file . '"'); readfile("$file"); } else { echo "No file selected"; } } ]]> Fri, 17 Oct 2014 06:26:43 +0200 <![CDATA[Minimal post slug ]]> function create_slug($string){ $slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string); return $slug; }]]> Fri, 17 Oct 2014 06:24:39 +0200 <![CDATA[Supression répertoire avec son contenu]]> /***** *@dir - Directory to destroy *@virtual[optional]- whether a virtual directory */ function destroyDir($dir, $virtual = false) { $ds = DIRECTORY_SEPARATOR; $dir = $virtual ? realpath($dir) : $dir; $dir = substr($dir, -1) == $ds ? substr($dir, 0, -1) : $dir; if (is_dir($dir) && $handle = opendir($dir)) { while ($file = readdir($handle)) { if ($file == '.' || $file == '..') { continue; } elseif (is_dir($dir.$ds.$file)) { destroyDir($dir.$ds.$file); } else { unlink($dir.$ds.$file); } } closedir($handle); rmdir($dir); return true; } else { return false; } } ]]> Fri, 17 Oct 2014 06:23:21 +0200 <![CDATA[Tronquer un texte (extrait)]]> function truncate($string, $length = 44, $etc = '...', $break_words = false, $middle = false) { if ($length == 0) return ''; if (strlen($string) > $length) { $length -= min($length, strlen($etc)); if (!$break_words && !$middle) { $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length + 1)); } if (!$middle) { return substr($string, 0, $length) . $etc; } else { return substr($string, 0, $length / 2) . $etc . substr($string, - $length / 2); } } else { return $string; } }]]> Fri, 17 Oct 2014 06:08:02 +0200 <![CDATA[Alternative a file_get_content pour les restriction des hébergeurs...]]> function file_get_contents_curl($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); $data = curl_exec($ch); curl_close($ch); return $data; }]]> Fri, 17 Oct 2014 06:03:35 +0200 <![CDATA[Supression des espace et des espace en double]]> function clean_trim($str) { // First remove the leading/trailing whitespace $str = trim($str); // Now remove any doubled-up whitespace $str = preg_replace('/\s(?=\s)/', '', $str); // Finally, replace any non-space whitespace, with a space $str = preg_replace('/[\n\r\t]/', ' ', $str); return $str; }]]> Fri, 17 Oct 2014 06:01:39 +0200 <![CDATA[Random String]]> /** * generate_rand * * chaine de caractère aléatoire. * * @param length nombre de caractères */ function generate_rand($length=4) { $chn = ''; for ($i = 1; $i <= $length; $i++) $chn .=chr(floor(rand(0, 25) + 97)); return $chn; }]]> Fri, 17 Oct 2014 05:58:01 +0200 <![CDATA[Chaîne de caractère aléatoire lisible]]> /************** * Générer une chaîne de caractère aléatoire *@length - longueur de chaîne aléatoire (doit être divisible par 2) **************/ function readable_random_string($length = 6){ $conso=array("b","c","d","f","g","h","j","k","l", "m","n","p","r","s","t","v","w","x","y","z"); $vocal=array("a","e","i","o","u"); $password=""; srand ((double)microtime()*1000000); $max = $length/2; for($i=1; $i<=$max; $i++) { $password.=$conso[rand(0,19)]; $password.=$vocal[rand(0,4)]; } return $password; } ]]> Fri, 17 Oct 2014 05:32:44 +0200