Fabrice Pigou

Mes fonctions

Convertir une chaine de caractères pour l'affichage dans l'url(slug)

function convertUrl($sString){
	$sString = transliterator_transliterate("Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();", $sString);
	$sString = preg_replace('# #', '-', $sString);
	$sString = preg_replace('#-$#', '', $sString);

	return $sString;
}
// Convertie les majuscules en minuscule, les "àçéè" etc en acee..., enlève les ponctuation, remplace les espaces " " par "-" et supprime les "-" en fin de chaine

Adresse IP d'un utilisateur

function getIp() {
	// IP si internet partagé
	if (isset($_SERVER['HTTP_CLIENT_IP'])) {
		return $_SERVER['HTTP_CLIENT_IP'];
	}
	// IP derrière un proxy
	elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
		return $_SERVER['HTTP_X_FORWARDED_FOR'];
	}
	// Sinon : IP normale
	else {
		return (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '');
	}
}

Le nom du mois

function monthName($month) {
	switch ($month) {	// on indique sur quelle variable on travaille
		case 1: $month	= 'Janvier';	break;
		case 2: $month	= 'Février';	break;
		case 3: $month	= 'Mars';		break;
		case 4: $month	= 'Avril';		break;
		case 5: $month	= 'Mai';		break;
		case 6: $month	= 'Juin';		break;
		case 7: $month	= 'Juillet';		break;
		case 8: $month	= 'Août';		break;
		case 9: $month	= 'Septembre';	break;
		case 10: $month = 'Octobre';		break;
		case 11: $month = 'Novembre';	break;
		case 12: $month = 'Décembre';	break;
		default: $month	= $month;
	}
	return $month;
}
ou
$months = ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'];


Le nom du jour

function monthName($day) {
	switch ($day) {	// on indique sur quelle variable on travaille
		case 1: $day	= 'Dimanche';	break;
		case 2: $day	= 'Lundi';		break;
		case 3: $day	= 'Mardi';		break;
		case 4: $day	= 'Mercredi';	break;
		case 5: $day	= 'Jeudi';		break;
		case 6: $day	= 'Vendredi';	break;
		case 7: $day	= 'Samedi';	break;
		default: $day	= $day;
	}
	return $day;
}
ou
$months = ['Dimanche', 'Lundi', 'Mardi', 'Mercredi',  'Jeudi', 'Vendredi', 'Samedi'];

Couper un chaîne de caractère après un mot
function truncate($text, $with) {
	$text = wordwrap($text, $width, '***');
	$text = explode('***', $text);
	return trim($text[0]);
}