SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

  1. #1
    Next stop: PHP! Marina's Avatar
    Join Date
    Apr 2001
    Location
    Sweden
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating new paragraphs in news script

    I'm using a form with a textfield in order to pubish news on my home page. The problem is that I can't press ENTER and begin a new paragraph because that paragraph will end up above all news entries and will be unformatted. It will look like this (look at the last entry, it continues above all the other entries).
    I'm using the script below to save my entries in a text file. Is there something I can add to that script that would make it possible to press ENTER when writing news without messing up the front page?

    This is the script Iím using to write news to a textfile:
    PHP Code:
    <?
    fopen
    ("news.txt""a+");
    if(
    $action == add){
    $fp fopen("news.txt""a+");

        
    $data="<span class=\"litenblue\">".date("d.m.Y")."</span> <br>";
        
    $data.="<span class=\"liten\">".$form_text."</span> </p> \n";

          
    fputs($fp$data);

            
    fclose($fp);
        echo 
    '
       <html>
       <head>
          <META HTTP-EQUIV="REFRESH" CONTENT="1; url=yo.phtml">
       </head>
       <body></body>
       </html>
       '
    ;
    }
    else{
    ?>

    <form name="textform" method="post" action="update.phtml" class="texten">
    <input type="hidden" name="action" value="add">
    <p> <span class="texten">
    <SCRIPT>
          if (navigator.appName == 'Netscape') {
            document.write('<textarea name="form_text" cols="25" rows="6" wrap="virtual"></textarea>\n');
          } else {
            document.write('<textarea name="form_text" cols="50" rows="6" class="formen" wrap="virtual"></textarea>\n');
          }
    </SCRIPT>
    </span></p>
    <p> <span class="texten">
    <input type="submit" name="Submit" value="Skicka!">
    </span></p>
    </form>

    <?
    }
    ?>
    I hope you understand what I mean.

  2. #2
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure if I fully understand your prob, but have you tried using:

    PHP Code:
    nl2br($text); 
    http://www.php.net/manual/en/function.nl2br.php

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  3. #3
    Next stop: PHP! Marina's Avatar
    Join Date
    Apr 2001
    Location
    Sweden
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I'm trying to do is write something like this in a textfield:

    Here comes my news text. Blah, blah, blah...
    Here is a new paragraph. Blah blah blah...

    I want it to display like this on the front page:

    Code:
    1.12.2001
    Here comes my news text. Blah, blah, blah...
    Here is a new paragraph. Blah blah blah...
    NOT like this:

    Code:
    Here is a new paragraph. Blah blah blah...
    
    1.12.2001
    Here comes my news text. Blah, blah, blah...
    But I think that you understood me correctly earlier. nl2br seems to be what I'm looking for. I should probably place it somewhere in this code: $data.="<span class=\"liten\">".$form_text."</span> </p> \n"; or am I wrong?

  4. #4
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The text is held in the variable $form_text, so do this:

    PHP Code:
    $form_text nl2br($form_text); 
    Put it before this part and all should work:

    PHP Code:
    $data="<span class=\"litenblue\">".date("d.m.Y")."</span> <br>";
    $data.="<span class=\"liten\">".$form_text."</span> </p> \n"
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  5. #5
    Next stop: PHP! Marina's Avatar
    Join Date
    Apr 2001
    Location
    Sweden
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much Sean! I'll try that later tonight and let you know how it works.

  6. #6
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How is the PHP learning going?

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  7. #7
    Next stop: PHP! Marina's Avatar
    Join Date
    Apr 2001
    Location
    Sweden
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *lol* Very slowly... I haven't purchased any books yet, but I'm learning from ready made scripts that I'm using right now (I'm designing a personal site for my boyfriend's sister). The PHP gurus (yes, you are one of them) here at SitePoint are also very helpful and I don't know what I would do without you. Thank you for all your help!

    For some reason $form_text = nl2br($form_text); didn't work in my script but I tried this instead: $form_text = ereg_replace("(\n)", "<br>", $form_text); and got the effect I needed.

  8. #8
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Marina
    The PHP gurus (yes, you are one of them) here at SitePoint are also very helpful and I don't know what I would do without you.
    I don't think I am!

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  9. #9
    Next stop: PHP! Marina's Avatar
    Join Date
    Apr 2001
    Location
    Sweden
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes you are! I see you helping out in the PHP forum all the time!

  10. #10
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Marina
    For some reason $form_text = nl2br($form_text); didn't work in my script but I tried this instead: $form_text = ereg_replace("(\n)", "<br>", $form_text); and got the effect I needed.
    Use str_replace() instead of ereg_replace() as it is faster

    http://www.php.net/manual/en/function.str-replace.php

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  11. #11
    Next stop: PHP! Marina's Avatar
    Join Date
    Apr 2001
    Location
    Sweden
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the tip. I'll do that.

  12. #12
    SitePoint Addict jough's Avatar
    Join Date
    Sep 2001
    Location
    You have moved into a dark place. It is pitch blac
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to add to an already solved problem...

    str_replace() is faster than ereg_replace() if you only need to replace one string with another - but of course if you needed to use the power of regular expressions (a huge topic on its own) you'd *need* to use ereg_replace() instead.

    Don't know why nl2br() didn't work for you. If you use:

    $form_text = nl2br($form_text); it returns the same thing that your string replace function does, except a bit faster.

    What is the value of $form_text after you use nl2br? Just curious...

    -- Jough
    My Dynamic Site in Action:
    Poetry X Poetry Archive


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •