par
sadeq » 16 mai 2006, 14:20
Moi j'utiliserais l'expression régulaire
preg_match comme ça:
<?php
class Personne {
private $nom;
private $prenom;
public function __construct($nom, $prenom) {
if ($this->verifieChaine($nom)) {
$this->nom = $nom;}
else{die('Le nom n est pas valide');}
if ($this->verifieChaine($prenom)) {
$this->prenom = $prenom;}
else{die('Le prenom n est pas valide');}
}
public function __tostring() {
return "Je suis $this->prenom $this->nom !";
}
public function afficheToi() {
print $this->__tostring();
}
public function getNom() {
return $this->nom;
}
public function getPrenom() {
return $this->prenom;
}
private function contientDesCaracteresAdmis($chaine) {
if ($chaine && (preg_match("#[a-zäàâëèéêïîöôûù]#", $chaine)) return true; else return false;
}
private function commenceParMajuscule($chaine) {
if ($chaine[0] && preg_match("#[A-Z]#", $chaine[0])) return true; else return false;
}
private function verifieChaine($chaine) {
if ($chaine && $this->commenceParMajuscule($chaine) && $this->contientDesCaracteresAdmis($chaine))
return true;
else return false;
}
}
//Test
//personne valide
$personne_valide = new Personne("Stehlin", "Alex");
$personne_valide->afficheToi();
//Dans ce qui suit, déactiver un test pour tester le suivant (puisque l'erreur fait un Die)
//Nom non valide (maj manque au début)
$personne_non_valide1 = new Personne("stehlin", "Alex");
$personne_non_valide1->afficheToi();
//Prénom non valide (maj manque au début)
$personne_valide2 = new Personne("Stehlin", "alex");
$personne_valide2->afficheToi();
//Nom non valide (caractères non admis)
$personne_valide2 = new Personne("123", "Alex");
$personne_valide2->afficheToi();
//Prénom non valide (caractères non admis)
$personne_valide2 = new Personne("Stehlin", "123");
$personne_valide2->afficheToi();
?>
Remarque: pour plus d'infos sur preg_match :
http://fr3.php.net/manual/fr/function.preg-match.php
Moi j'utiliserais l'expression régulaire [b]preg_match[/b] comme ça:
[php]
<?php
class Personne {
private $nom;
private $prenom;
public function __construct($nom, $prenom) {
if ($this->verifieChaine($nom)) {
$this->nom = $nom;}
else{die('Le nom n est pas valide');}
if ($this->verifieChaine($prenom)) {
$this->prenom = $prenom;}
else{die('Le prenom n est pas valide');}
}
public function __tostring() {
return "Je suis $this->prenom $this->nom !";
}
public function afficheToi() {
print $this->__tostring();
}
public function getNom() {
return $this->nom;
}
public function getPrenom() {
return $this->prenom;
}
private function contientDesCaracteresAdmis($chaine) {
if ($chaine && (preg_match("#[a-zäàâëèéêïîöôûù]#", $chaine)) return true; else return false;
}
private function commenceParMajuscule($chaine) {
if ($chaine[0] && preg_match("#[A-Z]#", $chaine[0])) return true; else return false;
}
private function verifieChaine($chaine) {
if ($chaine && $this->commenceParMajuscule($chaine) && $this->contientDesCaracteresAdmis($chaine))
return true;
else return false;
}
}
//Test
//personne valide
$personne_valide = new Personne("Stehlin", "Alex");
$personne_valide->afficheToi();
//Dans ce qui suit, déactiver un test pour tester le suivant (puisque l'erreur fait un Die)
//Nom non valide (maj manque au début)
$personne_non_valide1 = new Personne("stehlin", "Alex");
$personne_non_valide1->afficheToi();
//Prénom non valide (maj manque au début)
$personne_valide2 = new Personne("Stehlin", "alex");
$personne_valide2->afficheToi();
//Nom non valide (caractères non admis)
$personne_valide2 = new Personne("123", "Alex");
$personne_valide2->afficheToi();
//Prénom non valide (caractères non admis)
$personne_valide2 = new Personne("Stehlin", "123");
$personne_valide2->afficheToi();
?>
[/php]
Remarque: pour plus d'infos sur preg_match : http://fr3.php.net/manual/fr/function.preg-match.php