$string = $_POST['pgn']; //sets $string to the value of the entered pgn
$string = strtolower($string); //replaces all upercase letters with their lowercase counterparts
$string = stripslashes($string); //removes any slashes from $string to protect against malicious users
echo "thank you for submitted the pgn.<br>"; //acknowledges a pgn has been submited
preg_match_all('/\[(\w+) "([^"]+)"\]/', $string, $matches, PREG_SET_ORDER); //searches $string for a patter that looks like this: [x "y"] and places them into their own arrays
foreach($matches as $key => $value) //where array1=the entire value, array2=x and array3=y
{
$tag=$value[1]; //sets $tag to the value of x that was previously searched for
$data[$tag]=$value[2]; //creats a new array with a key matching x and the value matching y
}
$wName=$data['white']; //seperates white's full name
$bName=$data['black']; //seperates black's full name
$wRating=$data['whiteelo']; //seperates white's rating
$bRating=$data['blackelo']; //seperates black's rating
$eco=$data['eco']; //seperates the eco (opening reference)
$date=$data['date']; //seperates the entire date (usually in yyyy.mm.dd format)
$outcome=$data['result']; //seperates the result
if(preg_match('/,/', $wName)==1 OR preg_match('/ /', $wName)==1) //checks if there is a comma in the name (provided it is in either "last, first" or "first last" formats)
{
if(preg_match('/,/',$wName)==1) //checks if white's name is in "last, first" format
{
$wName=str_replace(" ","",$wName); //removes the space from white's name
$white_name=explode(",",$wName); //seperates white's first name and last name
if(count($white_name)< 3) //verifies there are only first and last name present
{
$wLast=$white_name[0]; //removes white's last name from the array
$wFirst=$white_name[1]; //removes white's first name from the array
}
else
{
echo "Error the format of black's name is incorrect. Please have black's name in either \"last, first\" or \"first last\" formats."; //warns the user that the name is incompatable
}
}
else //if it is not in "last, first" format it is assumed it is in "first last" format
{
$white_name=explode(" ",$wName);
if(count($white_name) < 3) //checks if there is more than 2 names found
{
$wFirst=$white_name[0]; //seperates white's white's first and
$wLast=$white_name[1]; //last name from the array
}
else
{
echo "Error the format of white's name is incorrect. Please have white's name in either \"last, first\" or \"first last\" formats."; //warns the user that the name is incompatable
}
}
}
else //if white's name is not in last, first or first last, that is there are no spaces or commas
{
$wFirst=$wName; //sets both white's first and
$wLast=$wName; //last name to the pgn's original value (usually an online handle)
}
if(preg_match('/,/', $bName)==1 OR preg_match('/ /', $bName)==1) //checks if there is a comma in the name (provided it is in either "last, first" or "first last" formats)
{
if(preg_match('/,/',$bName)==1) //checks if black's name is in "last, first" format
{
$bName=str_replace(" ","",$bName); //removes the space from black's name
$black_name=explode(",",$bName); //seperates black's first name and last name
if(count($black_name)< 3) //verifies there are only first and last name present
{
$bLast=$black_name[0]; //removes black's last name from the array
$bFirst=$black_name[1]; //removes black's first name from the array
}
else
{
echo "Error the format of black's name is incorrect. Please have black's name in either \"last, first\" or \"first last\" formats."; //warns the user that the name is incompatable
}
}
else //if it is not in "last, first" format it is assumed it is in "first last" format or another format with spaces
{
$black_name=explode(" ",$bName);
if(count($black_name) < 3) //checks if there is more than 2 names found
{
$bFirst=$black_name[0]; //seperates black's first and
$bLast=$black_name[1]; //last name from the array
}
else
{
echo "Error the format of black's name is incorrect. Please have black's name in either \"last, first\" or \"first last\" formats."; //warns the user that the name is incompatable
}
}
}
else //if black's name is not in last, first or first last, that is there are no spaces or commas
{
$bFirst=$bName; //sets both white's first and
$bLast=$bName; //last name to the pgn's original value (usually an online handle)
}
if($date!=="??" AND $date!=="*" AND $date!=="") //verifies that there is usable data in the pgn's date tag
{
$day=explode(".",$date); //seperates the date's year month and day in to an array
$year=$day['0']; //since the standard pgn date format is "yyyy.mm.dd" the first array value is used for year
if(strlen($year)!==4) //if the number of numbers in year is not four then the format of date must have been different
{
$year = $day['1']; //this sets the year to the second value of array $day, that is if the date is in "dd.yyyy.mm" or "mm.yyyy.dd" format
if(strlen($year)!==4) //if the number of numbers in $year is not for then the format must be "dd.mm.yyyy" or "mm.dd.yyyy"
{
$year = $day['2']; //sets the year to third value in array $day
}
}
}
else
{
$year=$date; //uses whatever the pgn's date tag says either
}
if($outcome !== "*" AND $outcome !== "??" AND $outcome!=="") //verifies there is usable data in the pgn's result tag
{
if($outcome == "0-1") //if black wins
{
$result = 0; //used to signify black wins
}
if($outcome == "1-0") //if white wins
{
$result = 1; //used to signify white wins
}
if($outcome == "1/2-1/2")//if the game is a draw
{
$result = 2; //used to signify a draw
}
}
else
{
$result = 3; //this will be used to signify that the result is still pending whether the game was adjourned or never completed
}
echo "$wFirst $wLast ($wRating) vs $bFirst $bLast ($bRating) $result $year";