Jump to content

Recommended Posts

 

php flatfile blog problem

Hi i was lucky to get help earlier so i try again . i a problem with the  flatfile script and that is that i cant make any line breaks in the form when i try to send data to the flatfile it displays all linebreaks as br after the server handle it. what i want to do is simply make the server handle the linebreaks as html and not text can that be done?

 

the code is

 

blog.php

 

<?

echo "<font face=verdana >";

$opFile = "blogfile.txt";

 

// Opens Blog File to read or dies

$fp = fopen($opFile,"r") or die("Error Reading File");

  $data = fread($fp, filesize($opFile));

fclose($fp);

 

// Explodes data at line breaks

$line = explode("\n", $data);

$i=count($line);

 

for ($n=0 ; $n < $i-1 ; $n++ ) {

  $blog = explode("|", $line[$n]);

 

  if (isset($blog[0]))

  {

    echo "name: " .$blog[0]."<br>";

    echo " date: " .$blog[1]."<br>";

    echo " subject: " .$blog[2]."<br>";

    echo " message: " .$blog[3]."<br><br>";

   

 

    }

}

 

  ?>

 

 

post.php the file i use the form to post to

<?php

 

$filename = "blogfile.txt";

 

if (!isset($meddelande)) {

$namn = $_POST['name'];

//$date = $_POST['date'];

$ämne = $_POST['subject'];

$message = $_POST['message'];

 

}

 

$postdate = date('d M Y');

 

$blog = $name."|".$postdate."|".$subject."|".$message."|[end]\n" ;

 

$data = fopen($filename, "a");

fwrite($data, $blog);

fclose($data);

echo "message sent";

?>

 

And ofcourse i got the post message and text file but i guess  you dont need that.

 

                              / lisa

 

Link to comment
https://forums.phpfreaks.com/topic/139591-phpflatfile-form-linebreak/
Share on other sites

nl2br

 

I believe is what you are looking for...

 

<?php

$filename = "blogfile.txt";

if (!isset($meddelande)) {
$namn = $_POST['name'];
//$date = $_POST['date'];
$ämne = $_POST['subject'];
$message = $_POST['message'];

}

$postdate = date('d M Y');

$blog = $name."|".$postdate."|".$subject."|".nl2br($message)."|[end]\n" ; // line modified here.

$data = fopen($filename, "a");
fwrite($data, $blog);
fclose($data);
echo "message sent";
?>

Maybe I mis-interpreted it...

 

Hi i was lucky to get help earlier so i try again . i a problem with the  flatfile script and that is that i cant make any line breaks in the form when i try to send data to the flatfile it displays all linebreaks as br after the server handle it. what i want to do is simply make the server handle the linebreaks as html and not text can that be done?

 

Do you not want it to display as <br>? Instead you would rather it display as \n and you want the HTML to display it like it would display in notepad or another text-editing application?

 

Can you provide an example of a post and how it displays then the same post with how it should display? I think that would help clear things up and help to get you the correct answer, cause I think you contradict yourself in your question, which is where my confusion is coming from.

I  think you got it right the first time want it to display as in a usual xhtml page when i hit a br in the form i want the line to break

like

text text text text text   

new line tezt text text

 

as it is now i get a long line without breaks but it displays the br html code right on the page

liket his text br

 

Scratch that update post.php and go back to your old one. Then use this to display the data.

 

<?php
echo "<font face=verdana >";
$opFile = "blogfile.txt";

// Opens Blog File to read or dies
$fp = fopen($opFile,"r") or die("Error Reading File");
  $data = fread($fp, filesize($opFile));
fclose($fp);

// Explodes data at line breaks
$line = explode("\n", $data);
$i=count($line);

for ($n=0 ; $n < $i-1 ; $n++ ) {
  $blog = explode("|", $line[$n]);

  if (isset($blog[0]))
   {
    echo "name: " .$blog[0]."<br>";
    echo " date: " .$blog[1]."<br>";
    echo " subject: " .$blog[2]."<br>";
    echo " message: " .nl2br($blog[3])."<br><br>";// changed this line here
    }
}
?> 

 

That should display it correctly with the brs in place.

thanks again, unfortunately it dont work for some reason im sure the code is right but it dont the work done.

Maybe its because im running php locally ?

 

Shouldn't be that problem. Can you post the first few lines of your blogfile.txt inside the [ code] and [ /code] tags (without initial space)

 

It could be that the \n's are throwing off the code and if I have it to test I can provide a more accurate response to the question.

thanks i appreciate it, the text file looks like this

 

lisa|05 Jan 2009|hello|text tezt text text[br] text text text|[end]

 

Is the br in there as

 

[br]

?

 

If so, this would work:

<?php
echo "<font face=verdana >";
$opFile = "blogfile.txt";

// Opens Blog File to read or dies
$fp = fopen($opFile,"r") or die("Error Reading File");
  $data = fread($fp, filesize($opFile));
fclose($fp);

// Explodes data at line breaks
$line = explode("\n", $data);
$i=count($line);

for ($n=0 ; $n < $i-1 ; $n++ ) {
  $blog = explode("|", $line[$n]);

  if (isset($blog[0]))
   {
    echo "name: " .$blog[0]."<br>";
    echo " date: " .$blog[1]."<br>";
    echo " subject: " .$blog[2]."<br>";
    echo " message: " .str_replace("[br]", "<br />", $blog[3])."<br><br>";// changed this line here
    }
}
?> 

 

I just wonder where/how it gets converted to a [ br]..as I do not see that in your code...

YES , you are the master . Thanks that did the job.

will you be here tomorrow i got some more problems with a login script and sessions? Or am i hopingfor to much?

maybe i could even add you to msn ? if you can find the time to help me with some more things i be happy to pay for your time

YES , you are the master . Thanks that did the job.

will you be here tomorrow i got some more problems with a login script and sessions? Or am i hopingfor to much?

 

 

Unless I call in sick, I should be.

 

Hehe, posting on the forums instead of doing your work...?  tisk tisk.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.