<?php
ini_set("display_errors", "on");
error_reporting(E_ALL ^ E_NOTICE);

$textListing = $_POST['textListing'];
$scrubType = $_POST['scrubType'];

$goodList = array("Dialog Table Entry");
$lines = array("\r\n", "\n", "\r");
$breaksIn = array("<", ">");
$breaksOut = array("&lt;", "&gt;");
if (!$textListing) {
    print "<form id=\"form1\" name=\"form1\" method=\"post\" action=\"Dialog-O-Matic.php\">
  <label>Dialog Text<br />
  <textarea name=\"textListing\" id=\"textListing\" cols=\"45\" rows=\"5\"></textarea>
  </label>
  <p>
    <label>
    <input type=\"radio\" name=\"scrubType\" id=\"scrubType\" value=\"none\" checked=\"checked\" />
    No scrub</label>
    <label>
    <input type=\"radio\" name=\"scrubType\" id=\"scrubType\" value=\"break\" />
    Scrub lines.</label>
    <label>
    <input type=\"submit\" name=\"Submit\" id=\"Submit\" value=\"Submit\" />
    </label>
    <label>
    <input type=\"reset\" name=\"Reset\" id=\"Reset\" value=\"Reset\" />
    </label>
  </p>
</form>";
} elseif ($scrubType == 'break') {
    $textListing = str_replace($lines, "[br]", $textListing);
    //$row = implode("[br]", $textListing);
    $data = explode('Dialog Table', $textListing);
    //$output = $array();
    foreach ($data as $key => $row) {
        $currow = explode("\t", $row, 3);
        //array_push($output, $currow[2]);
        print $currow[2]."\r\n<br />";
    }
} else {
    $data = array();
    $row = str_replace($lines, "|", $textListing);
    $row = str_replace($breaksIn, $breaksOut, $row);
    $array = explode("|", $row);
    $counter = 0;
    foreach($array as $count => $row) {
        $bad = array ('\t');
        $good = array ('|');
        //$row = addslashes($row);
        $row = preg_replace('/(\t)/', '|', $row);
        $row = preg_replace('/(\r)/', '', $row);
        $row = preg_replace('/(\n)/', '', $row);
        $stop = 1;
        while ($stop == 1){
            if (substr($row, -1) == "|") {
                $row = substr($row, 0, -1);
            } else {
                $stop = 0;
            }
        }
        $checkRow = explode("|", $row);
        foreach ($checkRow as $key => $rowPart) {
            if (substr($rowPart, 0, 1) == '"' && substr($rowPart, -1) == '"'){
                //print "row in: $rowPart ||| row Out: ";
                $rowPart = substr($rowPart, 1, -1);
                //print $rowPart."<br />\r\n";
            }
            $checkRow[$key] = str_replace('""', '"', $rowPart);
        }
        $newRow = implode("|", $checkRow);
        //print "<b>".substr($newRow, 0, 1)." | ".substr($newRow, -1)."</b><br />\r\n";
        if (substr($newRow, -2) == "|5") {
            $newRow = substr($newRow, 0, -2);
        }

        if ($checkRow[0] == $goodList[0]) {
            //print "\r\n<br /><b>New row</b> ";
            $data[$counter] = $newRow;
            //print $data[$counter]."<br /><br />\r\n";
            $counter++;
        } else {
            //print "$row \r\n<br />";
            $data[($counter-1)] .= "[br]".$newRow;
            //print $data[($counter-1)]."<br /><br />\r\n";
        }
    }
    foreach($data as $curRow) {
        $thisrow = explode('|', $curRow);
        print $thisrow[2]."<br />\r\n";
    }
}
    

