Browse Source

Fix bug in parser when there are hidden chars at beginning of pgn

master
Alf Magne Kalleland 2 years ago
parent
commit
d3773ab4fb
  1. 5
      PgnParser.php
  2. 15
      test/ParserTest.php
  3. 3140
      test/pgn/philadelphiainternational2019.pgn

5
PgnParser.php

@ -83,6 +83,7 @@ class PgnParser
$c = str_replace("0-0-0", "O-O-O", $c);
$c = str_replace("0-0", "O-O", $c);
$c = preg_replace('/^([^\[])*?\[/', '[', $c);
return $c;
}
@ -102,12 +103,16 @@ class PgnParser
$ret = array();
$content = "\n\n" . $pgn;
$games = preg_split("/\n\n\[/s", $content, -1, PREG_SPLIT_DELIM_CAPTURE);
file_put_contents("parsed.pgn", $content);
for ($i = 1, $count = count($games); $i < $count; $i++) {
$gameContent = trim("[" . $games[$i]);
if (strlen($gameContent) > 10) {
array_push($ret, $gameContent);
}
}
return $ret;
}

15
test/ParserTest.php

@ -2691,5 +2691,20 @@ $pgn = '[Event "URS-ch23"]
}
/**
* @test
*/
public function shouldImportAllGames(){
$pgn = file_get_contents("pgn/philadelphiainternational2019.pgn");
// when
$parser = new PgnParser();
$parser->setPgnContent($pgn);
$content = $parser->getUnparsedGames();
// when
// then
$this->assertEquals(132, $parser->countGames());
}
}

3140
test/pgn/philadelphiainternational2019.pgn

File diff suppressed because it is too large
Loading…
Cancel
Save