Hello again,
I put everything to the class clsPGNGame. So now the constructor reads the whole file to the array $games. It took me a while to figure out that this syntax: "$this->$games" is incorrect !
We have two methods:
PrintAllGames
PrintGame($gamenumber)
Similar two methods (without HTML tags") will be used later to creat a pgn file on fly. As you can noticed I did not return a big string from PrintAllGames and then print it, since I was concerned about performance again.
There is a real mystery here (mayby a bug ?) !!! Please check those lines:
while ... {
...
$gametext=&$this->games[$gamenum]["rawgame"];
...
//RF This one works (display data) when
//element 12 is reached inside of the loop !!!
echo "<br>{$this->games[12]["rawgame"]}<br>";
}
//RF This one does not work outside of the loop !!! The same syntax !!! No display !!!
echo "<br>{$this->games[12]["rawgame"]}<br>";
THE WHOLE CODE:
<?
//Settings
define("MAX_LINE_SIZE", 255);
//**************************************************************
// Class PGN - to handle chess PGN files
//**************************************************************
class clsPGNGame {
var $filename; //name of the PGN file
var $games; //Array of all PGN games.
//**************************************************************
// Class constructor, reads PGN file and sets variables
//**************************************************************
function clsPGNGame($filename) {
$fd = fopen ("$filename", "r");
//read one line, max line size=500
$line = fgets($fd, MAX_LINE_SIZE);
$gamenum=0;
while (!feof ($fd)) {
//removes any junk (empty lines, spaces) between header and game
// (there should be only one line, but just in case)
while (! ereg('[^\r\n\t] ',$line) && (!feof ($fd))) {
//read next line
$line = fgets($fd, MAX_LINE_SIZE);
}
//*****************************************************************
//Read Header
//*****************************************************************
//check if it is a tag line, if yes that means header starts here
if (ereg("\\[(.+) \"(.+)\"]", $line)) {
$gamenum++;
$tagnum=0;
//echo "<br> Game # $game <br><br>";
while (ereg("\\[(.+) \"(.+)\"]", $line, $matches) && (!feof ($fd))){
//read tags
$tagnum++;
$this->games[$gamenum]["tags"][$matches[1]]=$matches[2];
//$games[$gamenum]["tagnames"][$tagnum]=$matches[1];
//keys($games($gamenum)) will return a list of all the keys that
//$game uses
//Test if tag exist - isset($array['tagname'])
//read new line
$line = fgets($fd, 500);
}
//Total number of tags for this game:
$this->games[$gamenum]["tagnum"]=$tagnum;
//echo "<br>";
//echo "Total number of tags = $tagnum <br><br>";
}
//*****************************************************************
//Removes any junk (empty lines, spaces) between header and game
//(there should be only one line anyway, but just in case)
while (! ereg('[^\r\n\t] ',$line) && (!feof ($fd))) {
//read next line
$line = fgets($fd, MAX_LINE_SIZE);
}
//*****************************************************************
// Read game text
//*****************************************************************
//To avoid long names we use reference
$gametext=&$this->games[$gamenum]["rawgame"];
$gametext=$line;
while (!ereg(" ((1-0)|(0-1)|(1/2-1/2)|(\\*))\r?\n", $line) &&
(!feof ($fd))){
//next line
$line = fgets($fd, MAX_LINE_SIZE);
$gametext=$gametext.$line;
}
//Remove next line characters - span lines
$gametext=ereg_replace("[\n\r\t]+", "", $gametext);
//RF This one works when element 12 is reached
// inside of the loop !!!
echo "<br>{$this->games[12]["rawgame"]}<br>";
//*****************************************************************
if (!feof ($fd)) {
$line = fgets($fd, MAX_LINE_SIZE);
}
} //end main while loop
//RF This one does not work outside of the loop !!!
echo "<br>{$this->games[12]["rawgame"]}<br>";
fclose ($fd);
} //end PGNGame constructor
//***************************************************************************
function PrintGame($anum) {
//Display tags
foreach($this->games[$anum]["tags"] as $tagname => $tagvalue) {
echo "$tagname - $tagvalue<BR>\n";
}
//Display Game Text
echo "<br>";
echo $this->games[$anum]["rawgame"];
//echo "<br>{$this->games[12]["rawgame"]}<br>";
echo "<br><br>\n";
}
//***************************************************************************
function PrintAllGames() {
foreach($this->games as $gamenum => $game) {
//echo "Game # $gamenum <br><br>\n";
foreach($game["tags"] as $tagname => $tagvalue) {
echo "[$tagname = $tagvalue]<BR>\n";
}
//Display Game Text
echo "<br>{$this->games[$gamenum]["rawgame"]}<br><br>\n";
}
}
//***************************************************************************
}
//**************************************************************************
//end class
//**************************************************************************
?>
<?
//**************************************************************************
//Main Program
//**************************************************************************
$Game=new clsPGNGame("b52.pgn");
//Prints the game #2
//$Game->PrintGame(12);
//$Game->PrintAllGames();
?>