No ending delimiter '^' found
Bonjour,
Mon expression rationnelle semble marcher, mais je n'en suis pas sur car j'ai n warning.
La fonction suivante accepte uniquement [[:alpha:]] (en fonction du pays grace à setlocale) :
- retourne VRAI pour [jean] [élie] [jean-élie] [jean élie] [long-pont-sur-l'arrivée]
- retourne FAUX pour [les 2 rives] [mystere & compagnie] [1234]
Code:
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
|
function is_alpha_only($arg_str)
{
$str = trim($arg_str); //suppression espace avant ou après
if (strlen($str)<1) return false; // pas de chaine vide
setlocale(LC_TIME, get_lang_locale());// prise en charge des caractères en fonction du pays
$pattern ='^[[:alpha:]]'; // chercher tout caractère qui n'est pas alpha
if(preg_match($pattern, $str)==1) return false;// caractère non alpha trouvé
return true;
}
//--- test de la fonction
$arr["jean"]=true;
$arr["élie"]=true;
$arr["jean-élie"]=true;
$arr["jean élie"]=true;
$arr["long-pont-sur-l'arrivée"]=true;
$arr["les 2 rives"]=false;
$arr["mystere & compagnie"]=false;
$arr["1234"]=false;
foreach ($arr as $key=>$value)
{
if($value!=is_alpha_only($key)) echo "resultat erroné";
else echo "resultat correct";
echo " pour [".$key."]\n</br>";
} |
J'ai le résultat suivant :
resultat correct pour [jean]
resultat correct pour [élie]
resultat correct pour [jean-élie]
resultat correct pour [jean élie]
resultat correct pour [long-pont-sur-l'arrivée]
resultat erroné pour [les 2 rives]
resultat erroné pour [mystere & compagnie]
resultat erroné pour [1234]
mais avec un warning pour chaque ligne:
PHP Warning: preg_match() [<a href='function.preg-match'>function.preg-match</a>]: No ending delimiter '^' found in ...
Je dois avoir un pb de syntaxe que je ne parviens pas a trouver.