gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Help Wanted > wanted: custom php coding for the moon books project

#130402 - bobslack - Sun Jun 03, 2007 1:26 am

I am looking for someone to write a small online utility that people can paste text into, hit a button, and have the text reformatted to 40 characters width, and then download said output.

email me at btmullins@gmail.com
_________________
Brandon
Founder and WebMaster of The MoonBooks Project
Free classic literature and films on the Nintendo DS
http://moonbooks.net

#130403 - keldon - Sun Jun 03, 2007 1:39 am

You can do that with javascript or java (and of course php).

So would that be 40 characters width where line breaks only happen after 40 characters? What happens to the other line breaks, do they become spaces or are they ignored?

#130409 - bobslack - Sun Jun 03, 2007 2:13 am

they would be removed. check out one of the books at http://moonbooks.net to see what i mean. i basically reformatted plain project gutenberg files.
_________________
Brandon
Founder and WebMaster of The MoonBooks Project
Free classic literature and films on the Nintendo DS
http://moonbooks.net

#130503 - ScottLininger - Mon Jun 04, 2007 5:42 am

Code:
<?php

if ($_REQUEST['doDownload']) {
  $filename = "SomeBook.txt";
  header("Pragma: public");
  header("Expires: 0");
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  header("Content-Type: application/octet-stream");
  header( "Content-Disposition: attachment; filename=\"$filename\"");
  header( "Content-Description: File Transfert");
  echo ReformatToFixedWidth($_REQUEST['input'],40);
  die();
}

function ReformatToFixedWidth($str,$maxLineLength) {
 
  // replace return chars to correct for brower/os differences
  $str = str_replace("\r\n","\n",$str);
  $str = str_replace("\t","     ",$str);

  // mark the paragraph locations for now with a char not likely to be in the text (|)
  $str = str_replace("\n\n","|",$str);
  $str = str_replace("\n"," ",$str);
  $str = str_replace("  "," ",$str);
 
  $returnStr = "";
  $lastWordStart = 0;
  $lastLineStart = 0;

  // walk the text and find a word break that gets maximum words per line
  for ($i=0;$i<strlen($str);$i++) {
    $currentChar = substr($str,$i,1);
   
    $currentLineLength = $i - $lastLineStart;
    if ($currentChar == ' ') {
      $lastWordStart = $i+1;
    } elseif ($currentChar == '|') {
      $returnStr .= substr($str,$lastLineStart,$i-$lastLineStart)."\r\n\r\n";
      $lastWordStart = $i+1;
      $lastLineStart = $i+1;
    } elseif ($currentLineLength >= $maxLineLength) {
      $returnStr .= substr($str,$lastLineStart,$lastWordStart-$lastLineStart)."\r\n";
      $lastWordStart = $lastWordStart;
      $lastLineStart = $lastWordStart;
    }
  }
  return $returnStr;
}
?>
<html>

<form method="post">
<h3>Paste your book here:</h3>
<textarea name="input" rows="20" cols="40">Paste here.</textarea>
<P>
<input type="submit" value="Convert Now!" />
<input type="submit" name="doDownload" value="Convert and Download" />
</form>

<h3>See results here:</h3>
<textarea name="output" rows="20" cols="40"><?=ReformatToFixedWidth($_REQUEST['input'],40)?></textarea>

</html>


This should do ya.

-Scott

#130883 - bobslack - Sat Jun 09, 2007 12:00 am

that seems to cut off a portion of the text towards the bottom.
_________________
Brandon
Founder and WebMaster of The MoonBooks Project
Free classic literature and films on the Nintendo DS
http://moonbooks.net

#131668 - ScottLininger - Mon Jun 18, 2007 11:48 pm

bobslack wrote:
that seems to cut off a portion of the text towards the bottom.


Can you send me the text you're trying to use? Worked fine for a couple of Gutenberg files I grabbed randomly from their site.

Also, what browser are you using?

-Scott