Answers.Body: <p>Parsing PGN string is quite straightforward, as you can simply follow <a href="http://www.saremba.de/chessgml/standards/pgn/pgn-complete.htm" rel="nofollow noreferrer">the specification</a> [<a href="https://en.wikipedia.org/wiki/Portable_Game_Notation" rel="nofollow noreferrer">Wikipedia alternative</a>], and you can do it manually by processing it line by line.</p>
  <p>There are already existing libraries that do this, for example, since you are using PHP, this is one PGN parser in PHP: <a href="https://github.com/amyboyd/pgn-parser" rel="nofollow noreferrer">https://github.com/amyboyd/pgn-parser</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-20T09:44:53.137Z
Answers.LastActivityDate: 2021-03-20T09:44:53.137Z
Answers.UserDisplayName: justhalf
Body: <p>I'm sending a request to the chess.com API to <a href="https://api.chess.com/pub/player/hikaru/games/2021/03" rel="nofollow noreferrer">https://api.chess.com/pub/player/hikaru/games/2021/03</a></p>
  <p>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.</p>
  <p>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.</p>
  <p>I considered using a regex which would work in most cases, but I worry the problems that may occur with a weird username.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-03-19T06:08:37.663Z
Comments.Text: You can use existing libraries, like https://github.com/amyboyd/pgn-parser
Comments.UserDisplayName: justhalf
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-03-20T09:18:13.790Z
Comments.Text: @justhalf you might want to post that as an answer now, preferably with some explanation, so that it's not just a link.
Comments.UserDisplayName: Glorfindel
Tags: <programming><pgn>
Title: How to parse PGN string from an API with PHP?
ThreadText: # How to parse PGN string from an API with PHP?
  
  (Asked by: username_0 on 2021-03-19)
  
  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.
  
  **Tags:** programming, pgn
  
  > **Comments:**
  > **username_1**: You can use existing libraries, like https://github.com/amyboyd/pgn-parser  
  > **username_2**: @username_1 you might want to post that as an answer now, preferably with some explanation, so that it's not just a link.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-03-20. Score: 2 | Accepted answer
  
  Parsing PGN string is quite straightforward, as you can simply follow [the specification](http://www.saremba.de/chessgml/standards/pgn/pgn-complete.htm) [[Wikipedia alternative](https://en.wikipedia.org/wiki/Portable_Game_Notation)], 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>
  
  
  ---
  
  