Jump to content

Recommended Posts

Can someone help me? I am trying to parse a TXT file to remove a certain string of characters. Here is the file's contents:

 

[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

 

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

 

1. e4 e5 2. Nf3 Nc6........

 

Thank you!

Link to comment
https://forums.phpfreaks.com/topic/207875-parse-txt-file-with-php/
Share on other sites

$string = "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";

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

 

Should get you where you requested.

 

Output:

1. e4  e5  2. Nf3  Nc6  3. Bb5  a6  4. Ba4  Nf6  5. O-O  Be7  6. d3  b5  7. Bb3  d6  8. Ng5  O-O  9. Nd2  h6  10. Ndf3  hxg5  11. Nxg5  Nd4  12. Bd5  c6  13. c3  cxd5  14. cxd4  exd4  15. exd5  Nxd5  16. Qh5  Bxg5  17. Bxg5  Nf6  18. Qf3  Bg4  19. Qf4  Re8  20. Rae1  Be2  21. Bxf6  Qxf6  22. Qxf6  gxf6  23. Rxe2  Rxe2  24. h3  Rae8  25. Rc1  Rxb2  26. Kh2  Rxf2  27. Rc6  Ree2  28. Rc8+  Kg7  29. Kg3  Rxg2+  30. Kf4  Rxa2  31. Rc1  b4  32. Kf3  b3  33. h4  Rac2  34. Rb1  b2  35. h5  Rh2  36. Rg1+  Kh7  37. Kg4  Rc1  38. Rxc1  bxc1=Q  39. Kg3  Qg1+  40. Kf3  Rh3+  41. Ke4   0-1

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?

 

<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');
?>

$filename = isset($_POST['pgnfile'])?$_POST['pgnfile']:null; //trying to call the variable 'pgnfile' from the form above

if (!is_null($filename)) {
    if (file_exists($filename)) {
        $string = file_get_contents($filename);
   
        $output = preg_replace('~{.*}~U', '', $string);
        $newfile = $filename . "converted"; //trying to append the word converted to the old filename
        file_put_contents($newfile, $output);

        echo file_get_contents($newfile);
    }
}

 

Should treat you right. A key note, any variable inside of single quotes is take literally. IE:  '$one'  would echo $one instead of what was in the variable.  Also to access GET / POST data you have to use the superglobal variable $_GET or $_POST

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.