Page 1 of 1

Parse TXT file using PHP

Posted: Thu Jul 15, 2010 2:23 pm
by bound4h
I am trying to parse a TXT file to remove a certain string of characters. Here is the file's contents:

[text][Event "FICS rated standard game"]
[Site "FICS"]
[FICSGamesDBGameNo "232678145"]
[White "escarra"]
[Black "elivija"]
[WhiteElo "1577"]
[BlackElo "1756"]
[TimeControl "300+15"]
[Date "2009.08.31"]
[WhiteClock "0:05:00.000"]
[BlackClock "0:05:00.000"]
[ECO "C84"]
[PlyCount "81"]
[Result "0-1"]

1. e4 {[%emt 0.0]} e5 {[%emt 0.0]} 2. Nf3 {[%emt 2.304]} Nc6 {[%emt 1.703]} 3. Bb5 {[%emt 1.645]} a6 {[%emt 1.375]} 4. Ba4 {[%emt 1.816]} Nf6 {[%emt 1.578]} 5. O-O {[%emt 1.649]} Be7 {[%emt 9.047]} 6. d3 {[%emt 16.644]} b5 {[%emt 9.25]} 7. Bb3 {[%emt 1.7]} d6 {[%emt 19.453]} 8. Ng5 {[%emt 7.848]} O-O {[%emt 3.656]} 9. Nd2 {[%emt 32.157]} h6 {[%emt 1.828]} 10. Ndf3 {[%emt 37.154]} hxg5 {[%emt 6.375]} 11. Nxg5 {[%emt 1.27]} Nd4 {[%emt 54.375]} 12. Bd5 {[%emt 13.521]} c6 {[%emt 2.938]} 13. c3 {[%emt 25.264]} cxd5 {[%emt 31.453]} 14. cxd4 {[%emt 14.905]} exd4 {[%emt 14.578]} 15. exd5 {[%emt 7.107]} Nxd5 {[%emt 7.218]} 16. Qh5 {[%emt 12.054]} Bxg5 {[%emt 24.828]} 17. Bxg5 {[%emt 3.941]} Nf6 {[%emt 5.547]} 18. Qf3 {[%emt 6.093]} Bg4 {[%emt 27.25]} 19. Qf4 {[%emt 61.961]} Re8 {[%emt 72.844]} 20. Rae1 {[%emt 17.542]} Be2 {[%emt 19.36]} 21. Bxf6 {[%emt 126.822]} Qxf6 {[%emt 10.5]} 22. Qxf6 {[%emt 14.586]} gxf6 {[%emt 3.843]} 23. Rxe2 {[%emt 1.539]} Rxe2 {[%emt 2.265]} 24. h3 {[%emt 6.326]} Rae8 {[%emt 17.484]} 25. Rc1 {[%emt 9.823]} Rxb2 {[%emt 5.156]} 26. Kh2 {[%emt 6.435]} Rxf2 {[%emt 11.922]} 27. Rc6 {[%emt 10.228]} Ree2 {[%emt 7.656]} 28. Rc8+ {[%emt 4.025]} Kg7 {[%emt 1.813]} 29. Kg3 {[%emt 20.22]} Rxg2+ {[%emt 2.796]} 30. Kf4 {[%emt 8.0]} Rxa2 {[%emt 40.281]} 31. Rc1 {[%emt 34.16]} b4 {[%emt 4.813]} 32. Kf3 {[%emt 5.201]} b3 {[%emt 5.969]} 33. h4 {[%emt 16.199]} Rac2 {[%emt 10.469]} 34. Rb1 {[%emt 14.643]} b2 {[%emt 5.344]} 35. h5 {[%emt 6.386]} Rh2 {[%emt 25.047]} 36. Rg1+ {[%emt 16.05]} Kh7 {[%emt 5.531]} 37. Kg4 {[%emt 30.38]} Rc1 {[%emt 7.969]} 38. Rxc1 {[%emt 4.443]} bxc1=Q {[%emt 8.61]} 39. Kg3 {[%emt 3.555]} Qg1+ {[%emt 11.218]} 40. Kf3 {[%emt 25.04]} Rh3+ {[%emt 30.704]} 41. Ke4 {[%emt 7.596]} {White resigns} 0-1[/text]

I would like to REMOVE the {blah blah} pieces of the file. So, it should read:

[text]1. e4 e5 2. Nf3 Nc6........[/text]

How can I do this with PHP? I have looked into parsing files, but have NO clue where to start.

Thanks

Re: Parse TXT file using PHP

Posted: Thu Jul 15, 2010 2:46 pm
by AbraCadaver
Should work:

Code: Select all

preg_replace('/{[^}]*}/', '', file_get_contents('/path/to/file.txt'));

Re: Parse TXT file using PHP

Posted: Thu Jul 15, 2010 3:31 pm
by bound4h
Ok, so tell me if this looks right. I think the $pgnfile variable is wrong because I don't know how to call the form variable into the php script. Is everything else right?

Code: Select all

<form name="convertpgn" method="post">  //I did not include an action page because I want it to be a self-posting form.  Is this correct syntax then?
<table>
<tr>
<td>
<input type="text" name="pgnfile" />
</td>
<td>
</td>
</tr>
<tr>
<td>
<input type="reset" value="Clear" name="reset" />
</td>
<td>
<input type="submit" value="Submit" name="submit" />
</td>
</tr>
</table>
</form>
<br />
<br />


<?php 

$filename = $pgnfile; //trying to call the variable 'pgnfile' from the form above

$string = file_get_contents('$filename');

$output = preg_replace('~{.*}~U', '', $string);

$newfile = "$pgnfile.converted"; //trying to append the word converted to the old filename

file_put_contents($newfile, $output);

echo file_get_contents('$newfile');
?>