<?php

/*
//*******
//
//    filename: date.php
//    author: Zack Brown
//    date: 22nd May 2008
//
//*******
*/

//start session
session_start();

//check param for purging session
if(isset($_GET['purge']))
{
    
//purge session
    
$_SESSION = array();
}

//start html
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
echo 
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
echo 
"<head>\n";
echo 
"<title>Development</title>\n";
echo 
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n";
echo 
"<link rel=\"stylesheet\" type=\"text/css\" href=\"html_style.css\" />\n";

//start javascript
echo "<script type=\"text/javascript\">\n\n";

//start function
echo "\tfunction switch_url(url)\n";
echo 
"\t{\n";

//gather document
echo "\t\tvar url_input = document.getElementById('regex_string');\n";

//switch url
echo "\t\turl_input.value = url;\n";

//end function
echo "\t}\n\n";

//start function
echo "\tfunction switch_mask(url)\n";
echo 
"\t{\n";

//gather document
echo "\t\tvar mask_input = document.getElementById('regex_mask');\n";

//switch url
echo "\t\tmask_input.value = url;\n";

//end function
echo "\t}\n\n";

//end javascript
echo "</script>\n";

echo 
"</head>\n";
echo 
"<body>\n\n";

//page header
echo "<h1>URL Regex Testbed</h1>\n";
echo 
"<p>To test your regex, fill out the fields below.</p>\n";

/*
//*******
//
//    check for submitted form and display results
//
//*******
*/

//check session array
$_SESSION['regex_masks'] = (array_key_exists("regex_masks"$_SESSION) && is_array($_SESSION['regex_masks'])) ? $_SESSION['regex_masks'] : array();

//check for form submission
if(count($_POST) > 0)
{
    
//tidy up values
    
$_POST['regex_string'] = trim($_POST['regex_string']);
    
$_POST['regex_mask'] = trim($_POST['regex_mask']);
    
    
//array of matches
    
$matches = array();
    
    
//display info
    
echo "<h3>Result:</h3>\n";
    
    
//check string lengths
    
if(strlen($_POST['regex_mask']) > && strlen($_POST['regex_string']) > 0)
    {
        
//run regex
        
echo var_dump(eregi($_POST['regex_mask'], $_POST['regex_string'], $matches));
        echo 
"<pre>" print_r($matchestrue) . "</pre>\n";
        
        
//save regex mask
        
$_SESSION['regex_masks'][] = $_POST['regex_mask'];
    }
    else
    {
        
//no regex to run
        
echo "<p>Missing required inputs.</p>\n";
    }
}

//compile array of urls
$urls = array();
$urls[] = "google.co.uk";
$urls[] = "www.google.co.uk";
$urls[] = "www.google.co.uk/";
$urls[] = "http://google.co.uk";
$urls[] = "http://google.co.uk/";
$urls[] = "http://www.google.co.uk";
$urls[] = "http://www.google.co.uk/";
$urls[] = "https://www.google.co.uk/";

$urls[] = "google.com";
$urls[] = "www.google.com";
$urls[] = "www.google.com/";
$urls[] = "http://google.com";
$urls[] = "http://google.com/";
$urls[] = "http://www.google.com";
$urls[] = "http://www.google.com/";
$urls[] = "https://www.google.com/";

$urls[] = "http://www.amazon.com/gp/yourstore/home/ref=topnav_ys_vg";
$urls[] = "http://en.wikipedia.org/wiki/Apple_Computer";
$urls[] = "http://www.play.com/Games/Games/6-/RegionHome.html";
$urls[] = "http://www.nackvision.com/talk/showthread.php?t=1461";
$urls[] = "http://www.scarboroughsfuture.org.uk/docs/news/40.pdf";
$urls[] = "http://mcknee.deviantart.com/art/Firefly-64273315";
$urls[] = "http://uk.guides.ign.com/guides/748589/page_3.html";
$urls[] = "http://www.google.co.uk/search?hl=en&q=apple&btnG=Google+Search&meta=";

/*
//*******
//
//    display form
//
//*******
*/

//start form
echo "<form action=\"url_regex.php\" method=\"post\">\n\n";
echo 
"\t<fieldset>\n\n";

//timestamp input field
echo "\t\t<label for=\"regex_string\">\n";
echo 
"\t\tURL\n";
echo 
"\t\t<input type=\"text\" name=\"regex_string\" id=\"regex_string\" value=\"" . (count($_POST) > stripslashes($_POST['regex_string']) : "") . "\" />\n";
echo 
"\t\t</label>\n";

//timestamp mask input field
echo "\t\t<label for=\"regex_mask\">\n";
echo 
"\t\tRegex\n";
echo 
"\t\t<input type=\"text\" name=\"regex_mask\" id=\"regex_mask\" value=\"" . (count($_POST) > stripslashes($_POST['regex_mask']) : "^[https?://]*[www|a-zA-Z0-9_]*.[a-zA-Z0-9_]+.[a-zA-Z.]{2,}/?(.*)?$") . "\">\n";
echo 
"\t\t</label>\n";

//form submit
echo "\t\t<label for=\"form_submit\">\n";
echo 
"\t\t<input type=\"submit\" name=\"form_submit\" id=\"form_submit\" class=\"form_submit\" value=\"Regex It!\">\n";
echo 
"<p><a href=\"url_regex.php?purge=true\" title=\"Purge session\">Purge session</a>?</p>\n";
echo 
"\t\t</label>\n";

//end form
echo "\n\t</fieldset>\n";
echo 
"<form>\n";

//start wrapper for urls
echo "<div id=\"regex_urls\">\n";

//url header
echo "\t<h3>Test URLs:</h3>\n";

//start list of urls
echo "\t<ul>\n";

//print array of urls
foreach($urls as $key => $value)
{
    
//check for mid point
    
if(($key 8) == 0)
    {
        
//end list of urls
        
echo "\t</ul>\n\n";
        
        
//start list of urls
        
echo "\t<ul>\n";        
    }
    
    
//print url
    
echo "\t<li><a href=\"#\" title=\"Test this url?\" onclick=\"javascript:switch_url('$value');\">$value</a></li>\n";
}

//end list of urls
echo "\t</ul>\n\n";

//end wrapper for urls
echo "</div>\n";

//start wrapper for attempts
echo "<div id=\"regex_masks\">\n";

//regex attempts header
echo "\t<h3>Regex Strings:</h3>\n";

//total regex attempts
echo "<p><strong>Total:</strong> " count($_SESSION['regex_masks']) . "</p>\n";

//start list of regex masks
echo "\t<ul>\n";

//print array of regex masks
foreach(array_reverse($_SESSION['regex_masks']) as $key => $value)
{
    
//check for break point
    
if(($key 8) == 0)
    {
        
//end list of attempts
        
echo "\t</ul>\n\n";
        
        
//start list of attempts
        
echo "\t<ul>\n";        
    }
    
    
//print regex mask
    
echo "\t\t<li><a href=\"#\" title=\"Test this regex mask?\" onclick=\"javascript:switch_mask('$value');\">$value</a></li>\n";
}

//end list of regex masks
echo "\t</ul>\n\n";

//end wrapper for attempts
echo "</div>\n";

//end html
echo "\n</body>\n";
echo 
"</html>\n";

?>