IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Optimiser cette fonction de correction des domaines d'email


Sujet :

Langage PHP

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Autodidacte
    Inscrit en
    Octobre 2006
    Messages
    58
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Autodidacte

    Informations forums :
    Inscription : Octobre 2006
    Messages : 58
    Points : 38
    Points
    38
    Par défaut Optimiser cette fonction de correction des domaines d'email
    Bonjour,

    j'ai codé cette fonction ( a partir des erreurs constatés sur mon site) qui a pour but de corriger le domaine des émail afin de réduire les email en erreurs.

    Celle ci fonctionne mais j'aimerai des conseils pour optimiser cette dernière que je trouve pas propre du tout !

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    function corriger_domaine_email($ad_mail)
    {
    $domain = explode('@', $ad_mail); 
    $domaine = $domain[1];
     
    $gmail = array('gmail.fr','gmal.com','gmazil.com', 'gmail.como', 'gail.com', 'gamil.com', 'gemail.com');//
    $orange = array('ornage.fr','prange.fr','roange.fr', 'orage.fr');//
    $wanadoo = array(
    'wanado.fr',
    'xanadoo.fr',
    'wandoo.fr', 
    'wanadoio.fr', 
    'wanadoo.gr');//
    $free = array('freee.fr','fre.fr');//
    $hotmailcom=array('hotmail.col', 'homail.com', 'hotmai.com' );//
    $hotmailfr=array('hotmil.fr');//
    $livefr=array('live.fe');
     
     
    $sfr=array('sfr.ft', 'sfr.sr');//
    $alice=array('aliceadel.fr');//
    $yahoo==array('yahoo.co', 'yaoo.fr');//
     
     
     
    	 if (in_array($domaine,$gmail))
    	  {
    	$ad_mail = $domain[0].'@gmail.com';	
    	  }
    	elseif(in_array($domaine,$orange))
    	  {
    	 $ad_mail = $domain[0].'@orange.fr';
    	  }
    	  elseif(in_array($domaine,$wanadoo))
    	  {
    	 $ad_mail = $domain[0].'@wanadoo.fr';
    	  }
    	  elseif(in_array($domaine,$free))
    	  {
    	 $ad_mail = $domain[0].'@free.fr';
    	  }
    	  elseif(in_array($domaine,$hotmailcom))
    	  {
    	 $ad_mail = $domain[0].'@hotmail.com';
    	  }
    	  elseif(in_array($domaine,$hotmailfr))
    	  {
    	 $ad_mail = $domain[0].'@hotmail.fr';
    	  }
    	  elseif(in_array($domaine,$livefr))
    	  {
    	 $ad_mail = $domain[0].'@live.fr';
    	  }
     
    	elseif(in_array($domaine,$sfr))
    	  {
    	 $ad_mail = $domain[0].'@sfr.fr';
    	  }
    	  elseif(in_array($domaine,$alice))
    	  {
    	 $ad_mail = $domain[0].'@aliceadsl.fr';
    	  }
     
    return $ad_mail;
    }
    merci par avance

  2. #2
    Expert éminent sénior
    Avatar de rawsrc
    Homme Profil pro
    Dev indep
    Inscrit en
    Mars 2004
    Messages
    6 142
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Dev indep

    Informations forums :
    Inscription : Mars 2004
    Messages : 6 142
    Points : 16 545
    Points
    16 545
    Billets dans le blog
    12
    Par défaut
    Salut,

    dans ton code tu as oublié de traiter les erreurs relatives à yahoo

    Voici une version un peu plus propre :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    function corriger_domaine_email($ad_mail)
    {
    	$domains = array_merge(
    		array_fill_keys(array('gmail.fr', 'gmal.com', 'gmazil.com', 'gmail.como', 'gail.com', 'gamil.com', 'gemail.com'), 'gmail.com'),
    		array_fill_keys(array('ornage.fr', 'prange.fr', 'roange.fr'), 'orange.fr'),
    		array_fill_keys(array('wanado.fr', 'xanadoo.fr', 'wandoo.fr', 'wanadoio.fr', 'wanadoo.gr'), 'wanadoo.fr'),
    		array_fill_keys(array('hotmail.col', 'homail.com', 'hotmai.com'), 'hotmail.com'),
    		array_fill_keys(array('hotmil.fr'), 'hotmail.fr'),
    		array_fill_keys(array('live.fe'), 'live.fr'),
    		array_fill_keys(array('sfr.ft', 'sfr.sr'), 'sfr.fr'),
    		array_fill_keys(array('aliceadel.fr'), 'aliceadsl.fr'),
    		array_fill_keys(array('yahoo.co'), 'yahoo.com'),
    		array_fill_keys(array('yahoo.fr'), 'yahoo.fr')
    	);
     
    	$parts = explode('@', $ad_mail);
     
    	return (isset($domains[$parts[1]])) ? $parts[0].'@'.$domains[$parts[1]] : $ad_mail;
    }

  3. #3
    Membre éprouvé Avatar de Shuty
    Homme Profil pro
    Ingénieur en développement
    Inscrit en
    Octobre 2012
    Messages
    630
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur en développement
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Octobre 2012
    Messages : 630
    Points : 1 174
    Points
    1 174
    Par défaut
    Pour éviter justement ce genre de pépin, même si ça semble être une contrainte pour l'utilisateur, il est préférable pour le webmaster de vérifié les mails lors de la saisi (corriger le problème à la source).

    Lors de la soumission du formulaire, si tu ne veux pas que l'utilisateur vérifie le mail. Tu peux toujours récupérer le domaine du mail et faire un ping dessus. Si le ping ne retourne rien alors l'user à gaffé et là tu lui renvoi une erreur.
    Agence web Dim'Solution, créateur de solutions numériques
    Sites internet, ecommerce, logiciels, applications mobiles, référencement (SEO), plugin Prestashop, Magento, WordPress, Joomla!...

    Cours de trading gratuit | Envoyer des sms gratuitement | Envoyer des fax gratuitement | Plateforme de Fax à l'international

  4. #4
    Nouveau membre du Club
    Homme Profil pro
    Autodidacte
    Inscrit en
    Octobre 2006
    Messages
    58
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Autodidacte

    Informations forums :
    Inscription : Octobre 2006
    Messages : 58
    Points : 38
    Points
    38
    Par défaut
    l'idée c'est de rendre un peu plus intuitif le site, du coup plutôt que le laisser face a son ignorance du nom de domaine je lui corrige automatiquement.
    sur un de mes sites j'ai une population pas forcement a l'aise avec l'informatique.... et leur corriger gmail.fr en gmail.com est plus simple que de leur faire comprendre leur erreur

  5. #5
    Membre habitué
    Homme Profil pro
    Collégien
    Inscrit en
    Septembre 2014
    Messages
    65
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 21
    Localisation : Nouvelle-Zélande

    Informations professionnelles :
    Activité : Collégien

    Informations forums :
    Inscription : Septembre 2014
    Messages : 65
    Points : 158
    Points
    158
    Par défaut
    Si tu as envies d'optimiser le module ci-dessous codé à la va vite, tu peux regarder du côté du ranking à améliorer, mais il reste quand même cohérent et marche quasi-parfaitement.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    <?php
     
    	main();
     
    	function main()
    	{
    		$tmp = get_validMail("toto@gmai.f");
    		echo $tmp;
    	}
     
    	function get_validMail($mail)
    	{
    		$domains = get_ValidDomaines();
    		$domain = get_Domaine($mail);
    		$nb_domains = sizeof($domains);
    		$possible_domains = null;
    		$possible_pays = null;
    		$all_domains = null;
    		$all_pays = null;
    		$j = 0;
    		$k = 0;
    		for ($i = 0; $i < $nb_domains; $i++)
    		{
    			if (strcasecmp($domains[$i]['domaine'], $domain['domaine']) == 0
    				&& strcasecmp($domains[$i]['pays'], $domain['pays']) == 0)
    				return $mail;
    			else if (strcasecmp($domains[$i]['domaine'], $domain['domaine']) == 0
    				&& strcasecmp($domains[$i]['pays'], $domain['pays']))
    				$possible_pays[$k++] = $domains[$i]['pays'];
    			else if (strcasecmp($domains[$i]['domaine'], $domain['domaine'])
    				&& strcasecmp($domains[$i]['pays'], $domain['pays']) == 0)
    				$possible_domains[$j++] = $domains[$i]['domaine'];
    			$all_domains[$i] = $domains[$i]['domaine'];
    			$all_pays[$i] = $domains[$i]['pays'];
    		}
    		if (is_null($possible_domains) && is_null($possible_pays))
    			return set_validMail($domain, get_validDomain($all_domains, $domain['domaine']), 
    					get_validDomain($all_pays, $domain['pays']));
    		$f_domain = get_validDomain($possible_domains, $domain['domaine']);
    		$f_pays = get_validDomain($possible_pays, $domain['pays']);
    		return set_validMail($domain, $f_domain, $f_pays);
    	}
     
    	function set_validMail($domain, $f_domain, $f_pays)
    	{
    		$dom = null;
    		$pays = null;
     
    		if(is_null($f_domain['domaine']))
    			$dom = $domain['domaine'];
    		else
    			$dom = $f_domain['domaine'];
    		if (is_null($f_pays['domaine']))
    			$pays = $domain['pays'];
    		else
    			$pays = $f_pays['domaine'];
    		if (strcasecmp($f_domain['domaine'], 'gmail') == 0)
    			$pays = 'com';
    		return $domain['base'].'@'.$dom.'.'.$pays;
    	}
     
    	function get_validDomain($domains, $domain)
    	{
    		$nb_domains = sizeof($domains);
    		$count = 0;
    		$max_count = array('domaine' => null, 'max' => null);
    		for ($i = 0; $i < $nb_domains; $i++)
    		{
    			$size_domains = strlen($domains[$i]);
    			$count = 0;
    			for ($j = 0; $j < $size_domains; $j++)
    			{
    				$size_domain = strlen($domain);
    				for ($k = 0; $k < $size_domain; $k++)
    					{
    						if ($domains[$i][0] == $domain[0])
    							$count += 3;
    						if ($max_count['max'] <= $count)
    							{
    								$max_count['max'] = $count;
    								$max_count['domaine'] = $domains[$i];
    							}
    						if ($domains[$i][$j] == $domain[$k])
    							$count++;
    				}
    			}
    		}
    		return $max_count;
    	}
     
    	function get_Domaine($mail)
    	{
    		$size_mail = strlen($mail);
    		$dot = 0;
    		$is_at = false;
    		$is_dot = false;
    		$base = null;
    		$domain = array('base' => null, 'domaine' => null, 'pays' => null);
    		for ($i = 0; $i < $size_mail; $i++)
    			{
    				if ($is_dot == true)
    				{
    					$is_at = false;
    					$domain['pays'] .= $mail[$i];
    				}
    				else if ($mail[$i] == '.')
    					$is_dot = true;
    				else if ($is_at == true && $is_dot == false)
    					$domain['domaine'] .= $mail[$i];
    				else if ($mail[$i] == '@')
    					$is_at = true;
    				else
    					$base .= $mail[$i];
    			}
    		$domain['base'] = $base;
    		return $domain;
    	}
     
    	function get_ValidDomaines()
    	{
    		$domains[0] = null;
    		$domains_temoins = array('@gmail.com',
    					 '@orange.fr', 
    					 '@yahoo.fr', '@yahoo.com',
    					 '@hotmail.fr', '@hotmail.com', 
    					 '@wanadoo.fr'
    					 );
    		$nb_dom = sizeof($domains_temoins);
    		for ($i = 0; $i < $nb_dom; $i++)
    			$domains[$i] = get_Domaine($domains_temoins[$i]);
    		return $domains;
    	}
    ?>

Discussions similaires

  1. Cette fonction est elle correcte?
    Par atlaslion dans le forum Excel
    Réponses: 7
    Dernier message: 16/04/2015, 16h05
  2. Pourquoi cette fonction ne fonctionne pas correctement
    Par cabiste007 dans le forum VB 6 et antérieur
    Réponses: 10
    Dernier message: 09/04/2015, 21h08
  3. [SP-2007] Classer projets en fonction des domaines
    Par stellkar dans le forum SharePoint
    Réponses: 9
    Dernier message: 10/05/2011, 10h07
  4. Réponses: 6
    Dernier message: 26/07/2010, 09h29
  5. corrections des fonctions de sommes et #REF
    Par bigey3 dans le forum Excel
    Réponses: 5
    Dernier message: 20/07/2009, 21h29

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo