Created
March 4, 2016 12:51
-
-
Save mistergraphx/4fff102512b80531323a to your computer and use it in GitHub Desktop.
Un filtre pour formatter des prix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* filtre joli_prix | |
* | |
* formatter un prix de manière plus esthétique | |
* | |
* @param $decimals (avant|apres !default) place la decimal avant ou après la devise | |
* | |
*/ | |
function filtre_joli_prix($prix, $decimals='apres'){ | |
// Pouvoir débrayer la devise de référence | |
if (! defined('PRIX_DEVISE')) { | |
define('PRIX_DEVISE','fr_FR.UTF-8'); | |
} | |
// Pouvoir débrayer l'écriture de la devise par défaut | |
if (! defined('DEVISE_DEFAUT')) { | |
define('DEVISE_DEFAUT','€'); | |
} | |
setlocale(LC_MONETARY, PRIX_DEVISE); | |
$localconv = localeconv(); | |
$prix = money_format('%!i',$prix); | |
($localconv['mon_decimal_point']) ? $decimal_separator = $localconv['mon_decimal_point'] : $decimal_separator = '.'; | |
$money = explode($decimal_separator,$prix); | |
$num = $money[0]; | |
if($decimals == 'apres'){ | |
(isset($money[1])) ? $num .= '<span class="currency">'.DEVISE_DEFAUT.'</span><span class="decimals">'.$money[1].'</span>' : $num; | |
$prix = $num; | |
}else{ | |
(isset($money[1])) ? $num .= '<span class="decimals">'.$decimal_separator.$money[1].'</span>' : $num; | |
$prix = $num.'<span class="currency">'.DEVISE_DEFAUT.'</span>'; | |
} | |
return $prix; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment