|
-
Jan 18th, 2006, 09:01 AM
#1
Thread Starter
Lively Member
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
Last edited by FES Germany; Jan 18th, 2006 at 11:35 AM.
Reason: resolved
-
Jan 18th, 2006, 09:51 AM
#2
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");
-
Jan 18th, 2006, 10:19 AM
#3
Thread Starter
Lively Member
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
-
Jan 18th, 2006, 10:58 AM
#4
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
-
Jan 18th, 2006, 11:34 AM
#5
Thread Starter
Lively Member
Re: reading data from file
hey, thanks. That works!!!!
Thanks so much!
-
Jan 18th, 2006, 11:36 AM
#6
Re: reading data from file[resolved]
No worries!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|