|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
How to save a blank line in PHP?
Thanks for looking at this, Matt and everyone!
I have a quick question: does anyone know how to read a blank line in PHP. I have this code: ---------------------------- $theFile = "data/data.txt"; $fp = fopen($theFile,"w"); if ( !( $fp)) { echo "Error opening file!"; exit; } $line = $HTTP_POST_VARS['input']; fwrite($fp, $line); if(!fclose($fp)) { echo "Error closing file!"; exit; } $program = "data/data.txt"; $programPointer = fopen($program, "r"); if ( !($programPointer)) { echo "Error opening program"; exit; } while (!feof($programPointer)) { $line = fgets($programPointer, 4096); echo $line; } echo "</a>"; fclose($programPointer); --------------------------- As you see, this codes just creates a text area, saves the input and echos it back to the webpage. But if you try to put something like: line 1 line 2 line 4 (line 3 supposed to be a blank line) then it just echos back: line 1 line 2 line 4 with line 3 omitted. You can try this at : http://www.cs.virginia.edu/~hlp4z/indexxx.php Thanks a lot!!!! |
|
#2
|
|||
|
|||
|
RE: How to save a blank line in PHP?
I don't know exactly how to do it, but one thing I used to do with Javascript is hard code blank spaces using the HTML escape sequence * which translates into a blank space.
If you can somehow write a short if statement that says that if the line is blank, make the line read <a name=$line_num>*</a><br> then you'll have no problem. -Adam |
|
#3
|
|||
|
|||
|
RE: How to save a blank line in PHP?
Adam,
I tried it but it seemed like it didn't work like that in PHP --Phan, |
|
#4
|
|||
|
|||
|
RE: How to save a blank line in PHP?
Well, I don't have the rest of your code, so I don't know exactly what you're passing to this page, and I can't really test it. Plus, as of 11:50 this morning, your page isn't parsing because data.txt isn't chmod'ed properly. But here's another suggestion. Take the input of the entire entire textarea and capture the results into a variable. Now explode the variable into array separated at each line break. Then you can treat each line as an individual variable and simply run something like this little if statement:
if ((!isset($line)) || $line = "") { $line = " "; } else { $line = $HTTP_POST_VARS["$input[$linenum]"]; } where $input[$linenum] is the name of each element in your new array. -Adam |
|
#5
|
|||
|
|||
|
RE: How to save a blank line in PHP?
Ok, Adam
Here's my complete code for the page. Sorry it's quite long! ----------------------------- <html> <head><title>First PHP page I've ever built!</title></head> <font size=4><font color=red><b> Here's your file:</b></font></font> <BR> </html> <?PHP /* This is for the display of the file. */ $theFile = "data/data.txt"; $fp = fopen($theFile,"w"); if ( !( $fp)) { echo "Error opening file!"; exit; } $line = $HTTP_POST_VARS['input']; $line = str_replace("<","<",$line); $line = str_replace(">",">",$line); $linenumber = 2; $position=true; $offset = 0; echo "<a name="line1">"; while($position!=false) { if ($offset<=strlen($line)) $position=strpos($line, "rn", $offset); if ($position == false) break; $tricky = "</a><BR>nr<a name="line$linenumber"> "; $line=substr_replace($line, $tricky, $position, 2); $sz = strlen($tricky); $linenumber+=1; $offset=$position+$sz+1; } /************************Adam's suggestion************************/ /* foreach($line as $element) { $element = trim($element); $pieces = explode("<BR>", $element); if ((!isset($line)) || $line = "") { $line = " "; } else { $line = $HTTP_POST_VARS["$pieces"]; } }*/ /***************************end suggestion************************/ fwrite($fp, $line); if(!fclose($fp)) { echo "Error closing file!"; exit; } $program = "data/data.txt"; $programPointer = fopen($program, "r"); if ( !($programPointer)) { echo "Error opening program"; exit; } while (!feof($programPointer)) { $line = fgets($programPointer, 4096); echo $line; } echo "</a>"; fclose($programPointer); ?> <html> <body> <FORM ACTION="http://www.cs.virginia.edu/~hlp4z/disPart.php" METHOD="POST" NAME="testing"> Please enter your file here:<BR> <TEXTAREA NAME="input" COLS="40" ROWS="20"></TEXTAREA><BR><BR> <INPUT TYPE="submit" NAME="submit" VALUE="submit"><BR> </FORM> </body> </html> ----------------------------- You can check this code out at: http://www.cs.virginia.edu/~hlp4z/disPart.php As you see, besides echoing the input file back to the page, I'm also trying to make each line recognizable by html by putting the <a name="lineNumber"> tag in front of each line. I tried Adam's most recent suggestion, but it still doesn't work. Any more ideas? Please help me! Thanks, Adam, for keep giving me input!!! I appreciate your help. --Phan |
|
#6
|
|||
|
|||
|
RE: How to save a blank line in PHP?
This needs a second look. I noticed it also collapses multiple spaces into a single space, for example
"this (spaces........)Â*page" echoes as "this page" I can take a look, but I can tell you that exploding at the <br> won't work, since there's no actual HTML line break. I bet writing the PHP is, in this one case, the easy part. I think the real key is to determine how HTML and PHP view line breaks inside a textarea. You could try to explode at "n" but I don't think that would work either. I think the key lies in successfully using the <PRE> tag to echo the data exactly as it appears into the text file and then reading from the file at a certain line. I'd have to spend some time taking a stab at writing the code since I'm not the slickest at syntax or using functions I don't know well. -Adam |
|
#7
|
|||
|
|||
|
RE: How to save a blank line in PHP?
Please help me! I'm still so stuck at this! I've tried everything but the answer is still somewhere!
|
|
#8
|
|||
|
|||
|
RE: How to save a blank line in PHP?
Well, I don't know what to say..the script you have up works fine for me...if I put in
line1 line2 line4 it echos is back just like that..I did try and put in multiple spaces and it did condense it to one space. That is just HTML for you. What I would do is ereg_replace spaces (' ') with a non-breaking space( )..... |
|
#9
|
|||
|
|||
|
RE: How to save a blank line in PHP?
No, Matt!
Did you mean you tried: http://www.cs.virginia.edu/disPart.php This was the update page that I was talking about (with Adam) If you check out this page right now, you'll see what I'm talking about. I've put links to demonstrate what I want. Again if you put line 1 line 2 line 4 Then the link "Take me to line 3 of input file" should take me to ...line 3 of input file!, which is a blank line supposedly. But it doesn't, it takes me to line 4 instead. |
|
#10
|
|||
|
|||
|
RE: How to save a blank line in PHP?
So sorry for the confusion, but I meant the page:
http://www.cs.virginia.edu/~hlp4z/disPart.php |
|
#11
|
|||
|
|||
|
RE: How to save a blank line in PHP?
Try this:
Code:
<html>
<head>
<title>First PHP page I've ever built!</title>
</head>
<BODY>
<?PHP
/* This is for the display of the file. */
/* Check to see if they are submitting text */
if (isset($submit)) {
// Save the file
$theFile = "data/data.txt";
$fp = fopen($theFile,"w");
if ( !( $fp)) {
echo "Error opening file!";
exit;
}
fwrite($fp, $HTTP_POST_VARS['input']);
if(!fclose($fp)) {
echo "Error closing file!";
exit;
}
//
// Load and display the file
$fp = fopen($theFile, "r");
if ( !($fp)) {
echo "Error opening data file";
exit;
}
$LineNum = 1;
while (!feof($fp)) {
$line = fgets($fp, 4096);
printf("<A NAME="Line%d">%s</A>",
$LineNum++,
nl2br(htmlspecialchars($line))
);
}
fclose($fp);
} //EndIf (isset($submit))
echo "<FORM ACTION="$PHP_SELF" METHOD="POST" NAME="testing">" ;
?>
Please enter your file here:<BR>
<TEXTAREA NAME="input" COLS="40" ROWS="20"></TEXTAREA><BR><BR>
<INPUT TYPE="submit" NAME="submit" VALUE="submit"><BR>
</FORM>
<a href= "#line1"> Take me to line 1 of input file.</a><br>
<a href= "#line2"> Take me to line 2 of input file.</a><br>
<a href= "#line3"> Take me to line 3 of input file.</a><br>
<a href= "#line4"> Take me to line 4 of input file.</a><br>
</body>
</html>
|
|
#12
|
|||
|
|||
|
RE: How to save a blank line in PHP?
Hi,jkofsky
Thanks for your code. I tried to put it at: http://www.cs.virginia.edu/~hlp4z/tryitout.php but as you see, it says there is a parse error on line 36, which is the last line in the last while loop, i.e, the line: printf("<A NAME="Line%d">%s</A>", $LineNum++, nl2br(htmlspecialchars($line)) ); And I'm not very sure what the enl2br function does... Thanks again for following my long post! --Phan, Phan, |
![]() |
| Viewing: Codewalkers Forums > PHP Related > PHP Coding > How to save a blank line in PHP? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|
|