Skip to content

Instantly share code, notes, and snippets.

View sdeluce's full-sized avatar

Stéphane Deluce sdeluce

View GitHub Profile
@sdeluce
sdeluce / test_mail.php
Last active January 4, 2016 01:28
Vérifier une adresse mail
<?php
function is_valid_email($email, $test_mx = false) {
if(eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email))
if($test_mx)
{
list($username, $domain) = split("@", $email);
return getmxrr($domain, $mxrecords);
}
else
return true;
@sdeluce
sdeluce / gravatar.php
Last active January 4, 2016 01:29
Utiliser Gravatar
<?php
/*------------------------------------------------*\
@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"
@sdeluce
sdeluce / truncate.php
Last active January 4, 2016 01:29
Creer un texte d'intro d'un paragraphe.
<?php
function truncate($string, $max_length = 50, $replacement = '...', $trunc_at_space = false) {
$max_length -= strlen($replacement);
$string_length = strlen($string);
if($string_length <= $max_length)
return $string;
if( $trunc_at_space && ($space_position = strrpos($string, ' ', $max_length-$string_length)) )
$max_length = $space_position;
@sdeluce
sdeluce / distance.php
Created January 22, 2014 07:28
Calcul de distance entre deux coordonnées GPS
<?php
function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2) {
$theta = $longitude1 - $longitude2;
$miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
$miles = acos($miles);
$miles = rad2deg($miles);
$miles = $miles * 60 * 1.1515;
$feet = $miles * 5280;
$yards = $feet / 3;
$kilometers = $miles * 1.609344;
@sdeluce
sdeluce / odd_even.php
Last active January 4, 2016 02:29
Ajouter une class odd et even sur un élément qui se répète (Commentaire, article de blog ...)
<?php echo 'class="'.<?php ($c++%2==1) ? 'odd' : 'even'.'"'; ?>
@sdeluce
sdeluce / img_base64.php
Last active January 4, 2016 02:29
Encoder une image en base64
<?php
$img = file_get_contents('monimage.png');
$img64 = 'data:image/x-icon;base64,'.base64_encode($img);
echo '<img src="'.$img64.'"" />';
@sdeluce
sdeluce / generate_string.php
Created January 22, 2014 10:51
Génère une suite de caractères aléatoire
<?php
function generate_rand($l){
$c= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
srand((double)microtime()*1000000);
for($i=0; $i<$l; $i++) {
$rand.= $c[rand()%strlen($c)];
}
return $rand;
}
@sdeluce
sdeluce / parse_json.php
Created January 22, 2014 10:54
Parse Json
<?php
$json ='{"id":0,"name":"Ashley","surname":"Ford","Website":"http://papermashup.com"} ';
$array=json_decode($json);
// print the array
print_r($array);
echo $array->name;
@sdeluce
sdeluce / date_format.php
Created January 22, 2014 11:04
Formater une date en php
<?php
$now = '2014-01-22';
$now = date("D d M Y", strtotime($now));
echo $now;
@sdeluce
sdeluce / max_height.js
Created January 22, 2014 20:03
Pour que les colonnes d'un site fassent la même hauteurs.
var max_height = 0;
$("div.col").each(function(){
if ($(this).height() > max_height) { max_height = $(this).height(); }
});
$("div.col").height(max_height);