Results 1 to 6 of 6

Thread: reading data from file[resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    103

    Resolved 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

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: reading data from file

    PHP Code:
    $fileName 'blahblah.pgn';
    $fileHandle fopen($fileName'r');
    $data fread($fileHandlefilesize($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"); 

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    103

    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

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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, -1PREG_SPLIT_NO_EMPTY); 
    That will make $moves an array

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    103

    Re: reading data from file

    hey, thanks. That works!!!!


    Thanks so much!

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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
  •  



Click Here to Expand Forum to Full Width