-1

I'm sending a request to the chess.com API to https://api.chess.com/pub/player/hikaru/games/2021/03

It will return all the games played by Hikaru in March of 2021, the interesting data is in the PGN key, however it seems to be stored in a string making parsing and extracting the data relatively difficult.

I am currently using PHP to send my curl request but I can't find a reliable way to parse the data within the PGN key to extract data like, opening, Elo, and most importantly the actual moves.

I considered using a regex which would work in most cases, but I worry the problems that may occur with a weird username.

2
  • 2
    You can use existing libraries, like github.com/amyboyd/pgn-parser – justhalf Mar 19 at 6:08
  • @justhalf you might want to post that as an answer now, preferably with some explanation, so that it's not just a link. – Glorfindel Mar 20 at 9:18
2

Parsing PGN string is quite straightforward, as you can simply follow the specification [Wikipedia alternative], and you can do it manually by processing it line by line.

There are already existing libraries that do this, for example, since you are using PHP, this is one PGN parser in PHP: https://github.com/amyboyd/pgn-parser

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.