247 lines
8.4 KiB
PHP
Executable File
247 lines
8.4 KiB
PHP
Executable File
<?php
|
||
// --------------------------------------------------------
|
||
// Format des tel pour affichage : nettoie et mets espaces
|
||
// --------------------------------------------------------
|
||
function formatTelAff($tel) {
|
||
$debugLocal=0;
|
||
$debugMsgLocal='';
|
||
if (empty($tel)) {return ''; exit ;}
|
||
|
||
// Nettoyage : garder les chiffres et le +
|
||
// Enlever ., /, - , _ , \, espace
|
||
$tel = trim($tel);
|
||
$tel = preg_replace('/[^+\d]/', '', $tel);
|
||
|
||
// Si commence par +, format international
|
||
if (substr($tel, 0, 1) === '+') {
|
||
if (substr($tel, 0, 3) === '+33' && preg_match('/^\+33\d{9}$/', $tel)) {
|
||
// Le numéro commence par +33 et contient 9 chiffres après l'indicatif
|
||
// Découpe format +33 6 55 44 66 33
|
||
$telFormatte = preg_replace('/(\+33)(\d{1})(\d{2})(\d{2})(\d{2})(\d{2})/', '$1 $2 $3 $4 $5 $6', $tel);
|
||
}
|
||
if (substr($tel, 0, 1) === '+' && preg_match('/^\+\d{1,3}\d{4,14}$/', $tel)) {
|
||
// Le numéro commence par + et est suivi d'un indicatif pays (1 à 3 chiffres) et d'un numéro (4 à 14 chiffres)
|
||
// Découpe le numéro en groupes de 2 chiffres après l'indicatif
|
||
$telFormatte = preg_replace('/(\+\d{1,3})(\d{1})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})?/', '$1 $2 $3 $4 $5 $6 $7', $tel);
|
||
}
|
||
if (substr($tel, 0, 1) === '+' && preg_match('/^\+\d{2}\d{4,14}$/', $tel)) {
|
||
// Le numéro commence par + suivi de 2 chiffres (indicatif pays) et contient 4 à 14 chiffres pour le numéro local
|
||
// Découpe le numéro en groupes de 2 chiffres après l'indicatif
|
||
$telFormatte = preg_replace('/(\+\d{2})(\d{1})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})?/', '$1 $2 $3 $4 $5 $6 $7', $tel);
|
||
}
|
||
} else {
|
||
// Format national ou inconnu, découpage standard
|
||
$chunks = str_split($tel, 2);
|
||
$telFormatte = implode(' ', $chunks);
|
||
}
|
||
|
||
if ($debugLocal== '1') $debugMsgLocal .= monDebug(1,['tel nettoyé pour affichage ' => $telFormatte ],'');
|
||
|
||
return $telFormatte;
|
||
}
|
||
|
||
// --------------------------------------------------------
|
||
// Format des tel pour db : nettoie les séparaterrs : en base = longueur = 15 ( 3 de marge pour international )
|
||
// --------------------------------------------------------
|
||
function formatTelpourDB($tel) {
|
||
$debugLocal=0;
|
||
$debugMsgLocal='';
|
||
if (empty($tel)) {return ''; exit ;}
|
||
$tel = trim($tel);
|
||
// Nettoyage : garder les chiffres et le +
|
||
// Enlever ., /, - , _ , \, espace
|
||
$telNettoye = preg_replace('/[^+\d]/', '', $tel);
|
||
|
||
if ($debugLocal== '1') $debugMsgLocal .= monDebug(1,['tel nettoyé pour BD' => $telNettoye ],'');
|
||
|
||
return $telNettoye;
|
||
}
|
||
|
||
// ---------------------------------------------------
|
||
// Format des nombres
|
||
// ---------------------------------------------------
|
||
// a optimiser en bouclant sur la liste des langues
|
||
// ---------------------------------------------------
|
||
function formatNB($nombre, $lg) {
|
||
$debugLocal=0;
|
||
$debugMsgLocal='';
|
||
|
||
if ($debugLocal== '1') $debugMsgLocal .= monDebug (1,['avant' .$lg => $nombre ],'formatNB');
|
||
|
||
// Nettoyage de base
|
||
$nombre = trim($nombre); // Équivaut à rtrim(ltrim())
|
||
$nombre = str_replace(',', '.', $nombre); // Remplace virgule par point pour conversion
|
||
$nombre = str_replace(' ', '', $nombre); // Supprime tous les espaces
|
||
|
||
// Conversion en float
|
||
$nombre = (float)$nombre;
|
||
|
||
// Formatage selon la langue
|
||
switch (strtoupper($lg)) {
|
||
case 'MYSQL':
|
||
$nombre = number_format($nombre, 2, '.', '');
|
||
break;
|
||
|
||
case 'ENG':
|
||
$nombre = number_format($nombre, 2, '.', ',');
|
||
break;
|
||
|
||
case 'ESP':
|
||
case 'ITA':
|
||
case 'ALL':
|
||
$nombre = number_format($nombre, 2, ',', '.');
|
||
break;
|
||
|
||
case 'FRA':
|
||
default:
|
||
$nombre = number_format($nombre, 2, ',', ' ');
|
||
}
|
||
|
||
if ($debugLocal== '1') monDebug (1,['apres '.$lg => $nombre ],'formatNB');
|
||
return $nombre;
|
||
}
|
||
|
||
// ---------------------------------------------------
|
||
// Check caractères speciaux
|
||
// ---------------------------------------------------
|
||
// Vérifie s’il y a un caractère invalide dans la chaîne
|
||
// ---------------------------------------------------
|
||
function CheckValidName($str) {
|
||
$invalidChars = ['\\' , '/' , ':' , '*' , '?' , '"' , '<' , '>' , '|' , '&' , ';' , '$' , '#' , '{' , '}' , '§' , '%' , '£' , '[' , ']' ];
|
||
foreach ($invalidChars as $char) {
|
||
if (mb_strpos($str, $char) !== false) {
|
||
return $char;
|
||
}
|
||
}
|
||
|
||
return ''; // Aucun caractère invalide
|
||
}
|
||
|
||
// a tester remplacera le caracetre interdit
|
||
function CheckValidName2($str, $jsVarName = 'inputField') {
|
||
$invalidChars = ['\\' , '/' , ':' , '*' , '?' , '"' , '<' , '>' , '|' , '&' , ';' , '$' , '#' , '{' , '}' , '§' , '%' , '£' , '[' , ']'];
|
||
|
||
foreach ($invalidChars as $char) {
|
||
if (mb_strpos($str, $char) !== false) {
|
||
// Génère du JS interactif pour remplacer le caractère
|
||
echo "<script>
|
||
let choix = prompt(
|
||
'Le caractère interdit \"$char\" a été détecté.\\n' +
|
||
'Veuillez choisir un remplacement :\\n' +
|
||
'1: Underscore (_)\\n' +
|
||
'2: Tiret (-)\\n' +
|
||
'3: Supprimer le caractère',
|
||
'1'
|
||
);
|
||
|
||
let champ = document.getElementById('$jsVarName');
|
||
if (champ) {
|
||
let val = champ.value;
|
||
switch (choix) {
|
||
case '1':
|
||
val = val.split('$char').join('_');
|
||
break;
|
||
case '2':
|
||
val = val.split('$char').join('-');
|
||
break;
|
||
case '3':
|
||
val = val.split('$char').join('');
|
||
break;
|
||
default:
|
||
alert('Aucun remplacement effectué.');
|
||
}
|
||
champ.value = val;
|
||
}
|
||
</script>";
|
||
break; // Stop à la première occurrence
|
||
}
|
||
}
|
||
}
|
||
|
||
// ---------------------------------------------------
|
||
// Check date
|
||
// ---------------------------------------------------
|
||
function f_isValidDDMMYYYY($date) {
|
||
$ok = true;
|
||
$separateur = '';
|
||
|
||
// Vérifier le séparateur
|
||
if (strpos($date, '-') !== false) {
|
||
$separateur = '-';
|
||
} elseif (strpos($date, '/') !== false) {
|
||
$separateur = '/';
|
||
}
|
||
|
||
// Vérifier si le séparateur existe
|
||
if ($separateur !== '') {
|
||
// Vérifier le format DD-MM-YYYY ou YYYY-MM-DD
|
||
// Format DD-MM-YYYY : ^([0-9]{2})-(\d{2})-(\d{4})$
|
||
// Format YYYY-MM-DD : ^([0-9]{4})-(\d{2})-(\d{2})$
|
||
$pattern = '#^([0-9]{2})' . preg_quote($separateur) . '([0-9]{2})' . preg_quote($separateur) . '([0-9]{4})$#';
|
||
$patternRev = '#^([0-9]{4})' . preg_quote($separateur) . '([0-9]{2})' . preg_quote($separateur) . '([0-9]{2})$#';
|
||
|
||
// Si le format correspond au format DD-MM-YYYY
|
||
if (preg_match($pattern, $date, $matches)) {
|
||
// Extraction jour, mois, année
|
||
list($jour, $mois, $annee) = array($matches[1], $matches[2], $matches[3]);
|
||
|
||
// Vérifier la validité de la date avec checkdate
|
||
if (!checkdate($mois, $jour, $annee)) {
|
||
$ok = false;
|
||
}
|
||
}
|
||
// Si le format correspond au format YYYY-MM-DD
|
||
elseif (preg_match($patternRev, $date, $matches)) {
|
||
// Extraction année, mois, jour
|
||
list($annee, $mois, $jour) = array($matches[1], $matches[2], $matches[3]);
|
||
|
||
// Vérifier la validité de la date avec checkdate
|
||
if (!checkdate($mois, $jour, $annee)) {
|
||
$ok = false;
|
||
}
|
||
} else {
|
||
$ok = false;
|
||
}
|
||
} else {
|
||
$ok = false;
|
||
}
|
||
return $ok;
|
||
}
|
||
|
||
function unaccent(/*UTF-8*/ $in) {
|
||
// Vérifie si l'extension intl est disponible pour utiliser transliterator
|
||
if (function_exists('transliterator_transliterate')) {// PHP 5.4+ avec intl
|
||
$out = transliterator_create('NFD; [:Nonspacing Mark:] Remove; NFC')->transliterate($in);
|
||
}
|
||
// Si transliterator n'est pas disponible, essaye avec normalizer
|
||
else if (function_exists('normalizer_normalize')) {// PHP 5.3+ avec intl
|
||
$out = normalizer_normalize(preg_replace('/\p{Mn}+/u', '', normalizer_normalize($in, Normalizer::FORM_D)), Normalizer::FORM_C);
|
||
}
|
||
// Si aucune méthode n'est disponible, renvoie la chaîne d'origine
|
||
else {
|
||
$out = $in;
|
||
}
|
||
|
||
return $out;
|
||
}
|
||
|
||
function urlExist($url) {
|
||
// Vérification si l'URL est valide
|
||
if (filter_var($url, FILTER_VALIDATE_URL)) {
|
||
// Si l'URL est valide, retourner un message positif
|
||
// $Thefheaders = "URL est valide";
|
||
return true;
|
||
} else {
|
||
// Si l'URL n'est pas valide, retourner un message d'erreur
|
||
// $Thefheaders = "URL n'est pas valide";
|
||
return false;
|
||
}
|
||
}
|
||
|
||
function convToHtmsSpecial($val) {
|
||
if (!empty($val)) {
|
||
return htmlspecialchars($val, ENT_QUOTES, 'UTF-8');
|
||
}
|
||
}
|
||
|
||
?>
|