Advertisement
obernardovieira

Regular Expressions & Alternative Syntax

Sep 6th, 2015
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.84 KB | None | 0 0
  1. //I learned here http://regexone.com/
  2.  
  3. <!DOCTYPE HTML>
  4.  
  5. <html>
  6.  
  7.     <head>
  8.         <meta charset="UTF-8">
  9.     </head>
  10.  
  11.     <body>
  12.  
  13.         <?php if(isset($_GET['yourname'])):
  14.  
  15.             $pattern = "/[A-Z][a-z]* [A-Z][a-z]*/";
  16.             preg_match($pattern, $_GET['yourname'], $matches);
  17.             if(isset($matches[0]) && $_GET['yourname'] == $matches[0]): ?>
  18.  
  19.                 <p>Your name is "<?php echo $_GET['yourname']; ?>"</p>
  20.             <?php else: ?>
  21.  
  22.                 <p>Your name is invalid!</p>
  23.             <?endif;
  24.  
  25.             $pattern = '/([a-z0-9(._)]*)@([a-z0-9]*)\.([a-z(.)]*)/';
  26.             preg_match($pattern, $_GET['youremail'], $matches);
  27.  
  28.  
  29.             if(isset($matches[0]) && $_GET['youremail'] == $matches[0]): ?>
  30.  
  31.                 <p>Your e-mail is "<?php echo $_GET['youremail']; ?>"</p>
  32.             <?php else: ?>
  33.  
  34.                 <p>Your e-mail is invalid!</p>
  35.             <?endif;
  36.  
  37.             $pattern = '/(\d{2})(\d{7})/';
  38.             preg_match($pattern, $_GET['yournumber'], $matches);
  39.  
  40.  
  41.             if(isset($matches[0]) && $_GET['yournumber'] == $matches[0]): ?>
  42.  
  43.                 <p>Your number is "<?php echo $_GET['yournumber']; ?>"</p>
  44.  
  45.                 <?php
  46.                 switch($matches[1]):
  47.                 case 96:
  48.                     echo "<p>You are from cell phone service one!</p>";
  49.                     break;
  50.                 case 91:
  51.                     echo "<p>You are from cell phone service two!</p>";
  52.                     break;
  53.                 case 93:
  54.                     echo "<p>You are from cell phone service three!</p>";
  55.                     break;
  56.                 endswitch;
  57.             else: ?>
  58.  
  59.                 <p>Your number is invalid!</p>
  60.             <?endif;
  61.  
  62.         else: ?>
  63.  
  64.             <form action="register_regex.php" method="get">
  65.                 <input type="text" name="yourname" placeholder="Write your 'FirstName LastName'"><br>
  66.                 <input type="text" name="youremail" placeholder="Write your e-mail"><br>
  67.                 <!--I know we can use type="e-mail"! Thanks to HTML 5 -->
  68.                 <input type="text" name="yournumber" placeholder="Introduce your phone number"><br>
  69.                
  70.                 <input type="submit" value="Send">
  71.             </form>
  72.  
  73.         <?php endif; ?>
  74.  
  75.     </body>
  76.  
  77. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement