Exemple 4-2-1 «Input d'une chaîne de caractères» - Script PHPInitiation à la programmation avec le langage PHP, §4 Formulaire HTML
|
Input L'entrée des données se fait généralement par un formulaire HTML. Il faut créer deux fichiers:
1. Le formulaire HTMLPour accéder à l'exemple de formulaire, cliquer sur le lien donné ci-dessous. C'est dans le formulaire que se trouve le bouton d'envoi qui lance l'exécution du script PHP. Lorsque le formulaire est affiché dans votre navigateur, pour voir son code HTML, appelez "Afficher le code source de la page" dans votre navigateur, ce qui, selon l'environnement, peut s'obtenir avec le menu contextuel (bouton droit de la souris). Dans le code HTML, remarquer que <form method="POST" action="xxx.php"> contient le nom xxx du fichier PHP chargé du dépouillement. 2. Le traitement PHPLe code PHP est donné ci-dessous. <!DOCTYPE html> <html lang="fr"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="robots" content="NoIndex,NoFollow"> <title>Traitement de l'exemple 4-2-1</title> </head> <body> <h1>Traitement de l'exemple 4-2-1</h1> <h2>Input d'une chaîne de caractères</h2> <h3>Lire des chiffres romains</h3> <?php /* Acquisition des données */ $rom = filter_input(INPUT_POST, 'rom'); /* Validation des données et traitement des erreurs d'input. */ if(strlen($rom)==0){ echo "<h2>Erreur d'input</h2>" ."<p><b>Nombre en chiffres romains</b>: <i>est attendue une chaîne de caractères non vide.</i></p>" .'</body></html>'; exit; } $tr = str_split($rom); $nilleg = 0; for ($i=0; $i<count($tr); $i++){ if (!in_array($tr[$i], array('M', 'D', 'C', 'L', 'X', 'V', 'I'))){ $nilleg++; } } if($nilleg>0){ echo "<h2>Erreur d'input</h2>" ."<p><b>Nombre en chiffres romains</b>: <i> seules sont acceptées les lettres majuscules suivantes: M, D, C, L, X, V, I.</i></p>"; if ($nilleg>=2){ echo '<p>' .$nilleg ." caractères illégaux ont été rencontrés.</p>" .'</body></html>'; } else { echo '<p>' .$nilleg ." caractère illégal a été rencontré.</p>" .'</body></html>'; } exit; } /* Affichage des données */ echo "<p>Nombre donné en chiffres romains: " .$rom .'</p>'; /* Programme principal Il peut y avoir, dans l'ordre - jusqu'à 3 M consécutifs - CM ou D ou CD - jusqu'à 3 C consécutifs - XC ou L ou XL - jusqu'à 3 X consécutifs - IX ou V ou IV - jusqu'à 3 I consécutifs */ $n = 0; $rang = 0; if ($rang < strlen($rom)){ if ($rom[$rang]=='M'){ $n = $n+1000; $rang++; } } if ($rang < strlen($rom)){ if ($rom[$rang]=='M'){ $n = $n+1000; $rang++; } } if ($rang < strlen($rom)){ if ($rom[$rang]=='M'){ $n = $n+1000; $rang++; } } if ($rang<strlen($rom)-1 and $rom[$rang]=='C' and $rom[$rang+1]=='M'){ $n = $n+900; $rang = $rang + 2; } elseif ($rang<strlen($rom) and $rom[$rang]=='D'){ $n = $n+500; $rang++; } elseif ($rang<strlen($rom)-1 and $rom[$rang]=='C' and $rom[$rang+1]=='D'){ $n = $n+400; $rang = $rang + 2; } if ($rang < strlen($rom)){ if ($rom[$rang]=='C'){ $n = $n+100; $rang++; } } if ($rang < strlen($rom)){ if ($rom[$rang]=='C'){ $n = $n+100; $rang++; } } if ($rang < strlen($rom)){ if ($rom[$rang]=='C'){ $n = $n+100; $rang++; } } if ($rang<strlen($rom)-1 and $rom[$rang]=='X' and $rom[$rang+1]=='C'){ $n = $n+90; $rang = $rang + 2; } elseif ($rang<strlen($rom) and $rom[$rang]=='L'){ $n = $n+50; $rang++; } elseif ($rang<strlen($rom)-1 and $rom[$rang]=='X' and $rom[$rang+1]=='L'){ $n = $n+40; $rang = $rang + 2; } if ($rang < strlen($rom)){ if ($rom[$rang]=='X'){ $n = $n+10; $rang++; } } if ($rang < strlen($rom)){ if ($rom[$rang]=='X'){ $n = $n+10; $rang++; } } if ($rang < strlen($rom)){ if ($rom[$rang]=='X'){ $n = $n+10; $rang++; } } if ($rang<strlen($rom)-1 and $rom[$rang]=='I' and $rom[$rang+1]=='X'){ $n = $n+9; $rang = $rang + 2; } elseif ($rang<strlen($rom) and $rom[$rang]=='V'){ $n = $n+5; $rang++; } elseif ($rang<strlen($rom)-1 and $rom[$rang]=='I' and $rom[$rang+1]=='V'){ $n = $n+4; $rang = $rang + 2; } if ($rang < strlen($rom)){ if ($rom[$rang]=='I'){ $n = $n+1; $rang++; } } if ($rang < strlen($rom)){ if ($rom[$rang]=='I'){ $n = $n+1; $rang++; } } if ($rang < strlen($rom)){ if ($rom[$rang]=='I'){ $n = $n+1; $rang++; } } /* Affichage du résultat */ if ($rang != strlen($rom)){ echo "<h2>Erreur de syntaxe</h2>" ."<p>Le nombre donné en chiffres romains n'est pas valide. Il peut comporter, dans l'ordre,</p> <ul> <li>jusqu'à 3 M consécutifs</li> <li>CM ou D ou CD</li> <li>jusqu'à 3 C consécutifs</li> <li>XC ou L ou XL</li> <li>jusqu'à 3 X consécutifs</li> <li>IX ou V ou IV</li> <li>jusqu'à 3 I consécutifs</li> </ul>"; } else { echo "<p>Nombre correspondant: <b>" .$n .'</b></p>'; } ?> </body> </html> |
Contact | Accueil > PHP > Initiation |