Results 1 to 10 of 10

Thread: nl2br() causing problems

  1. #1
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts

    nl2br() causing problems

    PHP Code:

    <?
    if($HTTP_POST_VARS['submit']) {
        if(
    $HTTP_POST_VARS['password'] == 'pass') {
            if(!
    $HTTP_POST_VARS['name']) {
                echo 
    "You must enter a name";
                exit;
            }
            if(!
    $HTTP_POST_VARS['news']) {
                echo 
    "You must enter some news";
                exit;
            }
            if(
    strstr($HTTP_POST_VARS['name'],"|")) {
                echo 
    "Name cannot contain the pipe symbol - |";
                exit;
            }
            if(
    strstr($HTTP_POST_VARS['news'],"|")) {
                echo 
    "News cannot contain the pipe symbol - |";
                exit;
            }
            
    $fp fopen('includes/news.txt','a');
            if(!
    $fp) {
                echo 
    "Error opening file!";
                exit;
            }
            
    $line date("j F Y - h:i A") . "|" $HTTP_POST_VARS['name'] . "|" $HTTP_POST_VARS['news'] . "|" $HTTP_POST_VARS['title'];
            
    $line str_replace("\r\n","<BR>",$line);
            
    $line .= "\r\n";
            
    fwrite($fp$line);
            if(!
    fclose($fp)) {
                echo 
    "Error closing file!";
                exit;
            }        
        } else {
            echo 
    "Bad Password";
        }
    }

    ?>
    This is a simple script that i went through for someones website so they can submit news without the use of a database. It all works fine apart from the fact that everytime a comma occurs a backslash is added. I believe this is down to the str_replace function and as soon as i saw it i wanted to replace it with nl2br() but the problem is, when i do replace it with the nl2br() function is screws up the layout of the news completely.

    Any ideas?

  2. #2
    HEXUS.net Webmaster
    Join Date
    Jul 2003
    Location
    UK
    Posts
    3,108
    Thanks
    1
    Thanked
    0 times in 0 posts
    1) use $_POST not $HTTP_POST_VARS, it's easier to type
    2) the nl2br function will probably mess up the content because you;ve tried formatting it before it went in the database. nl2br() is used to take the text entered into a textarea field and redisplay it with the line breaks the user entered in the first place. In your instance you don't need nl2br as you're using text files
    3) why do you have the pipe check twice ?

  3. #3
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    1) ok using that global now
    2) so i put nl2br() around the textarea?
    3) It checks the news and the name so it's doing two different checks

  4. #4
    HEXUS.net Webmaster
    Join Date
    Jul 2003
    Location
    UK
    Posts
    3,108
    Thanks
    1
    Thanked
    0 times in 0 posts
    no, you shouldn't use nl2br in your case. Which field is getting the \ added to it ?

  5. #5
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    The news field.

    PHP Code:
    <?php
    $data 
    file('includes/news.txt');
    $data array_reverse($data);
    foreach(
    $data as $element) {
        
    $element trim($element);
        
    $pieces explode("|"$element);
        echo 
    '<div id="head">' $pieces[3] . '</div><div id="date">Posted by ' $pieces[1] . ' on ' $pieces[0] . '</div><br />' $pieces[2] . '<br /><br />';
    }
    ?>
    That's the output, it's pretty basic yet i still can't understand why the backslash is being added :X

  6. #6
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    So anyone have any idea why it's inserting a backslash everytime a comma occurs? I know it's an insertion operator but it shouldn't be doing it

  7. #7
    HEXUS.net Webmaster
    Join Date
    Jul 2003
    Location
    UK
    Posts
    3,108
    Thanks
    1
    Thanked
    0 times in 0 posts
    as I said in IRC, turn off magic_quotes_gpc in your php.ini file, restart your web server and see if that helps. Alternatively try the stripslashes() command

  8. #8
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    Right stripslashes() worked just fine. I don't have access to the .ini file and i wouldn't be able to restart the server so that was the only option. cheers.

    To save me making another thread i have a problem with a script exactly the same just doing a different job.

    PHP Code:
    <?
    if($_POST['submit']) {
        if(
    $_POST['password'] == 'pword') {
            if(!
    $_POST['date']) {
                echo 
    "You must enter a name";
                exit;
            }
            if(!
    $_POST['place']) {
                echo 
    "You must enter some news";
                exit;
        if(!
    $_POST['bands']) {
            echo 
    "You must enter a band";
            exit;
        if(!
    $_POST['ageprice']) {
            echo 
    "You must enter an age or price";
            exit;
            }
            if(
    strstr($_POST['date'],"|")) {
                echo 
    "Name cannot contain the pipe symbol - |";
                exit;
            }
            if(
    strstr($_POST['place'],"|")) {
                echo 
    "News cannot contain the pipe symbol - |";
                exit;
            }
            
    $fp fopen('includes/shows.txt','a');
            if(!
    $fp) {
                echo 
    "Error opening file!";
                exit;
            }
            
    $line $_POST['date'] . "|" $_POST['place'] . "|" $_POST['bands'] . "|" $_POST['ageprice'];
        
    $line str_replace("\r\n","<br />",$line);
            
    $line .= "\r\n";
            
    fwrite($fp$line);
            if(!
    fclose($fp)) {
                echo 
    "Error closing file!";
                exit;
            }        
        } else {
            echo 
    "Bad Password";
        }
    }

    ?>

    <form action="<?=$PHP_SELF?>" method="POST" name="addshows">

    date:<br />
    <input type="text" SIZE="30" name="date"><br /><br />

    place:<br />
    <input type="text" size="30" name="place"><br /><br />

    bands:<br />
    <input type="text" size="30" name="bands"><br /><br />

    ageprice:<br />
    <input type="text" size="30" name="ageprice"><br /><br />

    Password:<br />
    <input type="password" size="30" name="password"><br /><br />

    <input type="submit" name="submit" value="Post it!"><br />
    </form>
    I get an error on line 64 which is the very last line where it tells me "Parse error: parse error, unexpected $end" I can't seem to figure it out at all.

    Any suggestions?

  9. #9
    HEXUS.net Webmaster
    Join Date
    Jul 2003
    Location
    UK
    Posts
    3,108
    Thanks
    1
    Thanked
    0 times in 0 posts
    You've left two ifs unclosed

    PHP Code:
     if(!$_POST['place']) {
                echo 
    "You must enter some news";
                exit;
        if(!
    $_POST['bands']) {
            echo 
    "You must enter a band";
            exit; 
    should be

    PHP Code:
     if(!$_POST['place']) {
                echo 
    "You must enter some news";
                exit; }
      if(!
    $_POST['bands']) {
            echo 
    "You must enter a band";
            exit; } 

  10. #10
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    doh, and i was checking for ages as well. ta az

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Having a few new rig problems, please help!
    By sagramore in forum PC Hardware and Components
    Replies: 33
    Last Post: 03-08-2004, 06:34 PM
  2. Troubleshooting - Software & Driver problems
    By Steve in forum Help! Quick Relief From Tech Headaches
    Replies: 0
    Last Post: 09-07-2004, 06:30 PM
  3. New egg flash advert causing problems?
    By TiG in forum HEXUS Suggestions
    Replies: 2
    Last Post: 26-05-2004, 07:43 AM
  4. Start Up Problems - I'm stumped
    By RoGuE|SaBeR in forum PC Hardware and Components
    Replies: 7
    Last Post: 27-04-2004, 04:34 PM
  5. Svchostc problems – possible virus
    By Jimmy Little in forum Software
    Replies: 10
    Last Post: 10-12-2003, 10:27 AM

Posting Permissions

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