A PHP library to parse and write chess games in the portable game notation (PGN) format.
Via composer
composer require chesszebra/portable-game-notation
Reading a single PGN game from a string:
use ChessZebra\PortableGameNotation\Reader\StringReader;
$reader = new StringReader('1. e4 e5');
$tokenIterator = $reader->read();
Reading a single PGN game from a stream:
use ChessZebra\PortableGameNotation\Reader\StringReader;
$reader = new StreamReader(fopen('games.pgn', 'r'));
$tokenIterator = $reader->read();
Wriring a PGN game to a string:
use ChessZebra\PortableGameNotation\TokenIterator;
use ChessZebra\PortableGameNotation\Token\StandardAlgebraicNotation;
use ChessZebra\PortableGameNotation\Writer\StringWriter;
use ChessZebra\StandardAlgebraicNotation\Notation;
$tokenIterator = new TokenIterator([
new MoveNumber(1),
new StandardAlgebraicNotation(new Notation('e4')),
]);
$writer = new StringWriter();
$writer->write($tokenIterator);
$pgn = $writer->getPgn();
Wriring a PGN game to a stream:
use ChessZebra\PortableGameNotation\TokenIterator;
use ChessZebra\PortableGameNotation\Token\StandardAlgebraicNotation;
use ChessZebra\PortableGameNotation\Writer\Stream;
use ChessZebra\StandardAlgebraicNotation\Notation;
$tokenIterator = new TokenIterator([
new MoveNumber(1),
new StandardAlgebraicNotation(new Notation('e4')),
]);
$writer = new Stream(fopen('game.pgn', 'w'));
$writer->write($tokenIterator);
use ChessZebra\PortableGameNotation\Lexer\StringLexer;
$lexer = new StringLexer('1. e4');
$token = $lexer->getNextToken();
use ChessZebra\PortableGameNotation\Lexer\StreamLexer;
$lexer = new StreamLexer(fopen('my-games.pgn', 'r'));
$token = $lexer->getNextToken();
Please see CONTRIBUTING and CONDUCT for details.
If you discover any security related issues, please report them via HackerOne.
The MIT License (MIT). Please see License File for more information.