reading data from file[resolved]
i'm working on a small site about chess.
The standart format chess games are saved in is .pgn
Now i want to read this data and store it into variables so I can use it in my website. (It's like a gameviewer.)
anyway, a pgn file looks like this
Code:
[White "PlayerWhite"]
[Black "PlayerBlack"]
[Other comments]
1. e4 d6 2. d4 f5 3. f4 fxe4 4. Qg4 Bxg4 5. Nc3 Bd1
...
{black checkmated} 1-0
How can I read the Information in the brakets [] into variables and how can I read the moves. The problem is, that they don't have a fixed length.
Please help!
FES GERMANY
Re: reading data from file
PHP Code:
$fileName = 'blahblah.pgn';
$fileHandle = fopen($fileName, 'r');
$data = fread($fileHandle, filesize($fileName));
preg_match('#\[Black "([a-z]*)"\]#i', $data, $matches);
$black = $matches[1];
preg_match('#\[White "([a-z]*)"\]#i', $data, $matches);
$white = $matches[1];
$moves = trim(preg_replace('#(\[(.+?)\])#si', '', $data));
echo ('Black: '.$black."\r\n");
echo ('White: '.$white."\r\n");
echo ('Moves: '.$moves."\r\n");
Re: reading data from file
That's pretty good!
There's only one small problem left.
Is there a way to get the moves into an array, or print them in a for loop because I always need one move at a time.
like
e4
d6
d4
...
Thanks for the help penagate!!!!
FES_Germany
Re: reading data from file
Change the $moves = line to this
PHP Code:
$movesData = trim(preg_replace('#(\[(.+?)\])#si', '', $data));
$moves = preg_split('#[0-9]\. #', $movesData, -1, PREG_SPLIT_NO_EMPTY);
That will make $moves an array :)
Re: reading data from file
hey, thanks. That works!!!!
Thanks so much!
Re: reading data from file[resolved]