<!DOCTYPE html>
<html> 

<head> 
  <title>Testing regular expressions</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
  <meta charset="utf-8" />
</head> 

<body>

<?php

// first define functions
  
function print_form() {

    echo <<<END

           <form action="
$_SERVER[PHP_SELF]" method="post">
           
           <h2 class="center">Date Parsing Program</h2> 
           <hr />

    <div class="center">
    <p>Please enter a five digit zipcode:</p>
    <input type="number" name="zipcode" size="8" 
          pattern="^\d{5}$" title="Zip codes must be exactly five digits" />

    <p>Please enter a state code:</p>
    <input type="text" name="state" size="4" maxlength="2"
          required="required" pattern="[A-Z]{2}" 
          title="State must be two uppercase letters" />

    <p>Please use the form: 1974-12-03 05:12:56</p>
    <p>Please enter a date:</p>
    <input type="text" name="date" size="50">


    <input type="hidden" name="stage" value="process">

    <input type="submit" value="Submit">
    <input type="reset" value="Clear">
    
    </form>
    <hr />
END;

  }

function 
process_form() {

    
$date $_POST['date'];
    
$state $_POST['state'];
    
$zip $_POST['zipcode'];

     print 
"<p>Processing your submission of date $date. . .</p>";


    
preg_match('/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/',$date,$date_parts);
    
$ar preg_split('/[- :]/',$date);

    if (
preg_match('/^\d{5}$/',$zip)) {
        print 
"<p>Zip code is good.</p>";
    }
    else {
       print 
"<p>Problem with zip code.</p>";
    }
 
    echo <<<END
         <h3>preg_match version</h3>

         <ul><li>Month: 
$date_parts[2]</li>
             <li>Day: 
$date_parts[3]</li>
             <li>Year: 
$date_parts[1]</li>
             <li>Hour: 
$date_parts[4]</li>
             <li>Minutes: 
$date_parts[5]</li>
             <li>Seconds: 
$date_parts[6]</li>
         </ul>
         
         <h3>split version</h3>
         <ul><li>Month: 
$ar[1]</li>
             <li>Day: 
$ar[2]</li>
             <li>Year: 
$ar[0]</li>
             <li>Hour: 
$ar[3]</li>
             <li>Minutes: 
$ar[4]</li>
             <li>Seconds: 
$ar[5]</li>
         </ul>
      
         <h3>state and zip</h3>
         <p>state: 
$state</p>
         <p>zip: 
$zip</p>
END;

}

# main program

if (isset($_POST['stage']) && ('process' == $_POST['stage'])) {
    
process_form();
} else {
    
print_form();
}

?>

</body>
</html>