.
php
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);
}
php
<iframe width="100%" height="434" src="http://snipet.teknotit.com/index.php?embed=5dfbeb2158cea" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 19/12/2019
améliortion de php7
Null Coalesce Operator
php
https://daylerees.com/php-pandas-php7/
<iframe width="100%" height="218" src="http://snipet.teknotit.com/index.php?embed=5d0d9302e1a25" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 22/06/2019
apache exec commande as root
sudo visudo
===========
www-data ALL = NOPASSWD: /usr/bin/php
apache2 php
<iframe width="100%" height="254" src="http://snipet.teknotit.com/index.php?embed=5c536966a35e2" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 31/01/2019
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);
}
?>
php
https://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/
<iframe width="100%" height="1496" src="http://snipet.teknotit.com/index.php?embed=5c0c234570d95" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 08/12/2018
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);
}
php
<iframe width="100%" height="380" src="http://snipet.teknotit.com/index.php?embed=5b07801aa1370" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 25/05/2018
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 );
}
php wordpress
<iframe width="100%" height="272" src="http://snipet.teknotit.com/index.php?embed=5afcf90f56ada" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 17/05/2018
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
php ubuntu
<iframe width="100%" height="254" src="http://snipet.teknotit.com/index.php?embed=56097d653b2d9" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 28/09/2015
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)
);
}
php
<iframe width="100%" height="398" src="http://snipet.teknotit.com/index.php?embed=55a487dbe1b96" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 14/07/2015
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');
php wordpress
http://stephanis.info/2011/08/11/create-new-admin-account-in-wordpress-via-ftp/
<iframe width="100%" height="416" src="http://snipet.teknotit.com/index.php?embed=559f5cbac99be" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 10/07/2015
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);
}
}
php
<iframe width="100%" height="398" src="http://snipet.teknotit.com/index.php?embed=54665037e4c84" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 14/11/2014
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');
?>
php
<iframe width="100%" height="362" src="http://snipet.teknotit.com/index.php?embed=54495af2403ad" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 23/10/2014
Détecter les requette en AJAX
function is_ajax_request(){
return !(empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest');
}
fonctions php
<iframe width="100%" height="254" src="http://snipet.teknotit.com/index.php?embed=54409b4bdb4de" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 17/10/2014
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" />';
}
Api fonctions php
<iframe width="100%" height="416" src="http://snipet.teknotit.com/index.php?embed=54409a8f98ac5" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 17/10/2014
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";
}
}
fichier fonctions php
<iframe width="100%" height="452" src="http://snipet.teknotit.com/index.php?embed=54409a0e1a97d" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 17/10/2014
Minimal post slug
function create_slug($string){
$slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
return $slug;
}
fonctions php
<iframe width="100%" height="254" src="http://snipet.teknotit.com/index.php?embed=544099c382072" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 17/10/2014
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;
}
}
fonctions php repertoire
<iframe width="100%" height="830" src="http://snipet.teknotit.com/index.php?embed=544098c4c60d1" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 17/10/2014
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;
}
}
fonctions php
<iframe width="100%" height="524" src="http://snipet.teknotit.com/index.php?embed=5440951a6a642" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 17/10/2014
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;
}
fonctions php
<iframe width="100%" height="398" src="http://snipet.teknotit.com/index.php?embed=544094a3a27ba" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 17/10/2014
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;
}
fonctions php
<iframe width="100%" height="362" src="http://snipet.teknotit.com/index.php?embed=544093fdebc42" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 17/10/2014
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;
}
aleatoire fonctions php
<iframe width="100%" height="434" src="http://snipet.teknotit.com/index.php?embed=5440918b74687" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 17/10/2014
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;
}
aleatoire fonctions php
<iframe width="100%" height="524" src="http://snipet.teknotit.com/index.php?embed=54408b8d1dbdb" type="text/html"></iframe>
Texte seul - Permalink - Snippet public posté le 17/10/2014