Click to See Complete Forum and Search --> : PHP Autoformat


neil9999
08-08-2005, 06:59 AM
Hi,

I'm making an autoformatting thing where you type in plain text. At the moment it does titles and subtitles and it should do tables. indexold1 does tables but not when they're at the end. index does them at the end but it doesn't do anything else. There's an if...elseif...else thing but I doen't think it's doing any of them for the text and subtitles.

Here's the text that works fine in the old one:

Experiment to find the boiling point of water

Aim
To find the boiling point of water.

Method
We put some water into a saucepan. We turn the oven on full. We put a thermometer in the saucepan. We put the saucepan on the oven.

The temperature should gradually rise. Bubbles should start to form in the water. When the temperature on the thermometer stops, that is the temperature for the boiling point of water!

Results
Time 0 minutes 30 seconds 1 minute 1 minute 30 2 minutes
Temperature 30 degrees 55 degrees 80 degrees 100 degrees 100 degrees
The boiling point of water is 100 degrees celsius.

Conclusion
This is the boiling point because at this temperature the week bonds between the water molecules break.

And this only works in the new one:

Table
let num
a 1
b 2
c 3

I'd like both to work in the new one. There aren't any errors produced, it just doesn't work.

Any ideas?

Thanks,

Neil

bathurst_guy
08-08-2005, 07:26 AM
code?

neil9999
08-08-2005, 07:42 AM
Oh yeah sorry.

Neil

bathurst_guy
08-08-2005, 07:57 AM
Gee... my eyes are sore, im sorry lol too late for me on that one, that one put me to sleep lol, ill check it in the morning and if no one has replied ill give it a shot then. :S

bathurst_guy
08-08-2005, 08:10 PM
Ok, so its not putting the closing table tag, thats the only problem I believe.

I'm currently behind a firewall and cant upload a slight change ive made to see if thats the problem, so I'm just going to upload it to here and i should be home in a few hours and ill upload to my server and test it then, unless someone else wants to do it in the meantime.

bathurst_guy
08-08-2005, 11:26 PM
Yey, got it.
<html>
<head>
<title>Auto Format</title>
<style type="text/css">
h1{
font-size:170%;
font-weight:bold;
text-align:center;
text-decoration: underline;
}
h2{
font-size:125%;
font-weight:bold;
text-decoration: underline;
}
td{
border-style:solid;
border-width:2px 1px 2px 1px;
border-color:#000000;
}
table{
border-collapse: collapse;
border-style:solid;
border-width:3px;
border-color:#000000;
}
</style>
</head>
<body>
<?php
if(isset($_POST["formt"])){

$outp="";
$txta=$_POST["thetxt"];
$srch=array("\\\"","\'");
$rplc=array("\"","'");

$txta=str_replace($srch,$rplc,$txta);

$txta=explode("\n",$txta);

$txt=array_fill(0,count($txta),"");
$k=0;

for($j=0;$j<count($txta);$j++){

$txta[$j]=trim($txta[$j]);

if($txta[$j]!=="" && $txta[$j]!=="\n" && $txta[$j]!==null){

$txt[$k]=$txta[$j];

$k++;

}
}

$txt=array_slice($txt,0,$k);

$txt2=array_fill(0,count($txt),"");

if(strlen($txt[0])<80 || strlen($txt[1])<40){

$txt[0]="<h1>".$txt[0]."</h1>";

$txt2[0]="h1";
}

echo $txt[0]; // echo first line as a h1

for($i=1;$i<count($txt);$i++){ // from second line onwards

if(isset($txt[$i+1])){

if((substr_count($txt[$i]," ")>=1 && (substr_count($txt[$i+1]," ")>=1 || !(isset($txt[$i+1])))) || substr_count($txt[$i]," ")>=2){
$txt[$i]=(($txt2[$i-1]!=="table")?"<table>":"")."<tr><td>".preg_replace("/ {2,}/","</td><td>",$txt[$i])."</td></tr>";
$txt2[$i] = "table";

}


elseif((substr_count($txt[$i]," ")>=1 && !isset($txt[$i+1])) || substr_count($txt[$i]," ")>=2){
$txt[$i]=(($txt2[$i-1]!=="table")?"<table>":"")."<tr><td>".preg_replace("/ {2,}/","</td><td>",$txt[$i])."</td></tr>";
$txt2[$i]="table";

}

elseif(strlen($txt[$i])<40 && $txt2[$i-1]!=="h2"){

$txt[$i]="<h2>".$txt[$i]."</h2>";

$txt2[$i]="h2";

if($txt2[$i-1]=="table"){

$outp.="</table>";


}

}

else{

$txt[$i]="<p>".$txt[$i]."</p>";
$z = ($i - 1);

if($txt2[$z]=="table"){

$outp.="</table>";


}

}
}
$outp.=$txt[$i]."\n";


}

echo $outp;

}
else{
?>
<form method="post" action="index.php">
<p>Type the text below:</p>
<p><textarea style="width:95%;height:300px" name="thetxt"></textarea></p>
<p><input type="submit" value="Format!" name="formt" /></p>
</form>
<?php
}
?>
</body>
</html>
:D

neil9999
08-09-2005, 04:46 AM
Thanks that makes the experiment one work but the other doesn't. This is the code outputted:

<h1>Table</h1><table><tr><td>let</td><td>num</td></tr>
<tr><td>a</td><td>1</td></tr>
<tr><td>b</td><td>2</td></tr>
c 3

I've discovered that the last line never gets formatted, but if I increase the for loop by 1 it doesn't seem to work.

Thanks for your help,

Neil