New on DHTMLGoodies.com:

DHTML Chess Wordpress Plugin (Beta) . Participate in the beta testing of the DHTML Chess for WordPress plugin, and receive a free Commercial License worth USD99.

Download PHP Chess Parser

Bookmark and Share

Licensing

This script is distributed under the LGPL open source license.
Commercial licenses are also available. Some of these licenses also includes personal e-mail support for up to 1 year.

Download files

You can download the the chess parser from this zip file or from GitHub.

Classes:

  • PgnParser: Converts pgn files/strings to JSON format.
  • FenParser0x88: Parser for a chess game. Can be used to make moves, get notations, valid moves, fen position etc.

Key features of the PgnParser:

  • Unlimited variations
  • Supports custom startup position
  • Creates FEN position for all the moves.

This is the chess parser used in DHTML Chess at dhtmlchess.com

Example of use:

<?php $parser = new PgnParser('my-games.pgn'); echo json_encode($parser->getGames()); ?>

This will give you data in this format:

[ { "metadata":{ "blackelo":"2400", "opening":"Grunfeld", "time":"10:41:37", "variation":"Three Knights Variation", "whiteelo":"2400", "whitetype":"human", "blacktype":"human", "castle":1 }, "event":"Computer chess game", "site":"Somewhere", "date":"2013.01.19", "round":"?", "white":"White player", "black":"Black player", "result":"*", "eco":"D90", "timecontrol":"300", "termination":"unterminated", "plycount":"21", "fen":"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", "moves":[ { "m":"e4", "from":"e2", "to":"e4", "fen":"rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1" }, { "m":"d5", "variations":[ [ { "m":"e5", "comment":"Test comment before", "from":"e7", "to":"e5", "fen":"rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2" }, { "m":"Nf3", "comment":"Test comment after", "from":"g1", "to":"f3", "fen":"rnbqkbnr/pppp1ppp/8/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2" }, { "m":"Nc6", "from":"b8", "to":"c6", "fen":"r1bqkbnr/pppp1ppp/2n5/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3" }, { "m":"Bc4", "from":"f1", "to":"c4", "fen":"r1bqkbnr/pppp1ppp/2n5/4p3/2B1P3/5N2/PPPP1PPP/RNBQK2R b KQkq - 3 3" }, { "m":"Nf6", "variations":[ [ { "m":"d6", "from":"d7", "to":"d6", "fen":"r1bqkbnr/ppp2ppp/2np4/4p3/2B1P3/5N2/PPPP1PPP/RNBQK2R w KQkq - 0 4" }, { "m":"O-O", "comment":"(O-O Ng8-f6 Nb1-c3 Nc6-d4 Nf3-g5 d6-d5 e4xd5 h7-h6 Ng5-e4 c7-c6 d5xc6 Nf6xe4 Nc3xe4 b7xc6) +0.48/11 8", "from":"e1", "to":"g1", "fen":"r1bqkbnr/ppp2ppp/2np4/4p3/2B1P3/5N2/PPPP1PPP/RNBQ1RK1 b kq - 1 4" }, { "m":"Nf6", "from":"g8", "to":"f6", "fen":"r1bqkb1r/ppp2ppp/2np1n2/4p3/2B1P3/5N2/PPPP1PPP/RNBQ1RK1 w kq - 2 5" }, { "m":"Nc3", "comment":"Last move in variation", "from":"b1", "to":"c3", "fen":"r1bqkb1r/ppp2ppp/2np1n2/4p3/2B1P3/2N2N2/PPPP1PPP/R1BQ1RK1 b kq - 3 5" } ] ], "from":"g8", "to":"f6", "fen":"r1bqkb1r/pppp1ppp/2n2n2/4p3/2B1P3/5N2/PPPP1PPP/RNBQK2R w KQkq - 4 4" }, { "m":"O-O", "comment":"Variation ended", "from":"e1", "to":"g1", "fen":"r1bqkb1r/pppp1ppp/2n2n2/4p3/2B1P3/5N2/PPPP1PPP/RNBQ1RK1 b kq - 5 4" }, { "m":"d6", "from":"d7", "to":"d6", "fen":"r1bqkb1r/ppp2ppp/2np1n2/4p3/2B1P3/5N2/PPPP1PPP/RNBQ1RK1 w kq - 0 5" } ] ], "from":"d7", "to":"d5", "fen":"rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2" }, { "m":"exd5", "comment":"Variation ended", "from":"e4", "to":"d5", "fen":"rnbqkbnr/ppp1pppp/8/3P4/8/8/PPPP1PPP/RNBQKBNR b KQkq - 0 2" }, { "m":"Nf6", "from":"g8", "to":"f6", "fen":"rnbqkb1r/ppp1pppp/5n2/3P4/8/8/PPPP1PPP/RNBQKBNR w KQkq - 1 3" }, { "m":"c4", "variations":[ [ { "m":"Nf3?!", "from":"g1", "to":"f3", "fen":"rnbqkb1r/ppp1pppp/5n2/3P4/8/5N2/PPPP1PPP/RNBQKB1R b KQkq - 2 3" }, { "m":"Qxd5", "from":"d8", "to":"d5", "fen":"rnb1kb1r/ppp1pppp/5n2/3q4/8/5N2/PPPP1PPP/RNBQKB1R w KQkq - 0 4" }, { "m":"Nc3", "from":"b1", "to":"c3", "fen":"rnb1kb1r/ppp1pppp/5n2/3q4/8/2N2N2/PPPP1PPP/R1BQKB1R b KQkq - 1 4" }, { "m":"Qa5", "from":"d5", "to":"a5", "fen":"rnb1kb1r/ppp1pppp/5n2/q7/8/2N2N2/PPPP1PPP/R1BQKB1R w KQkq - 2 5" }, { "m":"d4", "from":"d2", "to":"d4", "fen":"rnb1kb1r/ppp1pppp/5n2/q7/3P4/2N2N2/PPP2PPP/R1BQKB1R b KQkq - 0 5" } ] ], "from":"c2", "to":"c4", "fen":"rnbqkb1r/ppp1pppp/5n2/3P4/2P5/8/PP1P1PPP/RNBQKBNR b KQkq - 0 3" }, { "m":"Bf5", "comment":"Variation ended", "variations":[ [ { "m":"Nbd7", "from":"b8", "to":"d7", "fen":"r1bqkb1r/pppnpppp/5n2/3P4/2P5/8/PP1P1PPP/RNBQKBNR w KQkq - 1 4" }, { "m":"Nf3", "from":"g1", "to":"f3", "fen":"r1bqkb1r/pppnpppp/5n2/3P4/2P5/5N2/PP1P1PPP/RNBQKB1R b KQkq - 2 4" }, { "m":"Nb6", "from":"d7", "to":"b6", "fen":"r1bqkb1r/ppp1pppp/1n3n2/3P4/2P5/5N2/PP1P1PPP/RNBQKB1R w KQkq - 3 5" }, { "m":"d4", "from":"d2", "to":"d4", "fen":"r1bqkb1r/ppp1pppp/1n3n2/3P4/2PP4/5N2/PP3PPP/RNBQKB1R b KQkq d3 0 5" }, { "m":"g6", "from":"g7", "to":"g6", "fen":"r1bqkb1r/ppp1pp1p/1n3np1/3P4/2PP4/5N2/PP3PPP/RNBQKB1R w KQkq - 0 6" }, { "m":"Nc3", "from":"b1", "to":"c3", "fen":"r1bqkb1r/ppp1pp1p/1n3np1/3P4/2PP4/2N2N2/PP3PPP/R1BQKB1R b KQkq - 1 6" }, { "m":"Qd6", "from":"d8", "to":"d6", "fen":"r1b1kb1r/ppp1pp1p/1n1q1np1/3P4/2PP4/2N2N2/PP3PPP/R1BQKB1R w KQkq - 2 7" } ] ], "from":"c8", "to":"f5", "fen":"rn1qkb1r/ppp1pppp/5n2/3P1b2/2P5/8/PP1P1PPP/RNBQKBNR w KQkq - 1 4" }, { "m":"Nf3", "from":"g1", "to":"f3", "fen":"rn1qkb1r/ppp1pppp/5n2/3P1b2/2P5/5N2/PP1P1PPP/RNBQKB1R b KQkq - 2 4" } ] } ]

Example: Make moves

FenParser0x88 can be used to manually make moves on the board.

Example:

$parser = new FenParser0x88(); $parser->setFen('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'); $parser->move("g1f3"); echo $parser->getNotation(); // Returns Nf3 echo $parser->getFen(); // Returns new fen position

Example: Get valid moves

This is an example on how to retrieve all valid moves in a position:

$parser = new FenParser0x88('6k1/6p1/4n3/8/8/8/B7/6K1 b - - 0 1'); $validBlackMoves = $parser->getValidMovesBoardCoordinates("black"); echo json_encode($validBlackMoves);

which outputs

{"g8":["f7","h7","f8","h8"],"g7":["g6","g5"],"e6":[]}

Here, the king on g8 can move to the squares f7, h7, f8, h8.

In this position, there's a black knight on e6 which is pinned by a white bishop on a2. That's why it doesn't have any squares to move to ("e6": []).

Comments

ozgur
Hi, I tried 3 days installing dhtml chess but cant succeed. Please can you publish a manual or tutorial about this. I put the folder in xampp and test from chrome. I cant succed to load a pgn file for tactics training
ozgur at 12:09PM, 2013/08/30.
Nick
Broken on first attempt :(

[Event "ProofOfConcept"]
[Site "Exploit"]
[Date "2015.??.??"]
[Round "?"]
[White "N.N."]
[Black "N.N."]
[Result "1-0"]
[Annotator ""]
[SetUp "1"]
[FEN "8/7P/8/8/1k15/8/P7/K7 w - - 0 1"]
[PlyCount "1"]
[EventDate "2015.??.??"]
[EventType "game"]
[EventCountry "GER"]

1. a3+
Nick at 11:22AM, 2015/09/12.
DHTMLGoodies
Nick,This problem has been resolved.It was caused by the 1k15 rank.When I first made this script, I noticed that some programs allowed fen strings like:rnbqkbnr/pppppppp/32/PPPPPPPP/RNBQKBNR w KQkq - 0 1instead ofrnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1i.e. 32 empty squares. So in your example, the parser looked for 15 empty squares and not 6(1+5).I tested with some chess programs today and they reported rnbqkbnr/pppppppp/32/PPPPPPPP/RNBQKBNR w KQkq - 0 1as invalid fen. As a result of this, I have changed the script today to only supports one digit numbers for empty squares. Your example parsed correctly after that modification.Best regards,AlfDHTMLGoodies.com
DHTMLGoodies at 12:24AM, 2015/12/24.
Guterkraftlicht19!
Good morning!! Thanks!!!!!
Guterkraftlicht19! at 05:54PM, 2016/02/16.
veerasekari
<a href="#comment4943">Nick wrote:</a>Broken on first attempt :([Event "ProofOfConcept"][Site "Exploit"][Date "2015.??.??"][Round "?"][White "N.N."][Black "N.N."][Result "1-0"][Annotator ""][SetUp "1"][FEN "8/7P/8/8/1k15/8/P7/K7 w - - 0 1"][PlyCount "1"][EventDate "2015.??.??"][EventType "game"][EventCountry "GER"]1. a3+
veerasekari at 04:33AM, 2016/11/05.
Nenad
Fatal error: Call to undefined method FenParser0x88::getValidMovesBoardCoordinates() in C:\xampp\htdocs\Testovi\chess-parser\test.php on line 30I can't find function getValidMovesBoardCoordinates() in Class FenParser0x88
Nenad at 08:53AM, 2016/11/20.
Nenad
Fatal error: Uncaught exception 'Exception' with message 'Fen: 5rk1/5p1p/1q4p1/8/8/8/6PP/Q3R2K b - - 0 48 color: black notation: bxa1... FenParser0x88.php on line 101248. ... bxa1=Q trigered exception.
Nenad at 04:31PM, 2016/11/20.
Nenad
Notice: Undefined offset: 120 in C:\xampp\htdocs\Testovi\chess-parser\FenParser0x88.php on line 291This notice is allways present.
Nenad at 07:32PM, 2016/11/20.
DHTMLGoodies
Nenad,The zip has been updated with a bugfix for this.
DHTMLGoodies at 11:40PM, 2016/12/25.

Post your comment

Don't have an avatar? Create one at Gravatar.com.

Confirmation code:

Go to cbolson.com


About/Contact | A good idea? | Submit a script | Privacy notice
© 2005 - 2017 dhtmlgoodies.com