PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old March 19th, 2002, 06:08 AM
phan phan is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 9 phan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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!!!!

Reply With Quote
  #2  
Old March 19th, 2002, 04:29 PM
sethadam1 sethadam1 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Orlando, FL
Posts: 223 sethadam1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
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

Reply With Quote
  #3  
Old March 19th, 2002, 10:10 PM
phan phan is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 9 phan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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,

Reply With Quote
  #4  
Old March 21st, 2002, 01:49 AM
sethadam1 sethadam1 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Orlando, FL
Posts: 223 sethadam1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
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 = "&nbsp;"; }
else {
$line = $HTTP_POST_VARS["$input[$linenum]"]; }

where $input[$linenum] is the name of each element in your new array.

-Adam

Reply With Quote
  #5  
Old March 21st, 2002, 04:22 AM
phan phan is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 9 phan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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 = "&nbsp;"; }
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

Reply With Quote
  #6  
Old March 21st, 2002, 07:29 AM
sethadam1 sethadam1 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Orlando, FL
Posts: 223 sethadam1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
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

Reply With Quote
  #7  
Old March 24th, 2002, 04:28 AM
phan phan is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 9 phan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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!

Reply With Quote
  #8  
Old March 24th, 2002, 04:44 AM
Matt Matt is offline
Contributing User
Codewalkers Specialist (4000 - 4499 posts)
 
Join Date: Apr 2007
Location: Florida
Posts: 4,158 Matt User rank is Private First Class (20 - 50 Reputation Level)Matt User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 12 m 16 sec
Reputation Power: 7
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(&nbsp;).....


Reply With Quote
  #9  
Old March 24th, 2002, 07:29 AM
phan phan is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 9 phan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #10  
Old March 24th, 2002, 07:30 AM
phan phan is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 9 phan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #11  
Old March 29th, 2002, 07:42 PM
jkofsky jkofsky is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 4 jkofsky User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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>

Reply With Quote
  #12  
Old March 29th, 2002, 08:15 PM
phan phan is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 9 phan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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,

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > How to save a blank line in PHP?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek