Formatting text?

Discussion in 'PHP' started by crazyryan, Jul 31, 2007.

  1. #1
    Hi, I've got a news script I'm working on.

    When I type into the text box:

    "Hello.

    This is a test."

    I'd like the script to automatically add <br /> where the new line is.

    How can I do this?
     
    crazyryan, Jul 31, 2007 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You can use php's built-in function nl2br.

    
    $newstring = nl2br($string);
    
    PHP:
     
    jestep, Jul 31, 2007 IP
  3. espmartin

    espmartin Well-Known Member

    Messages:
    1,137
    Likes Received:
    46
    Best Answers:
    0
    Trophy Points:
    160
    #3
    Sorry to bust in on this thread, but can you, Jestep, create a very small example
    of this php code, with a bit more detail?

    Like an actual (very small) "working model"?
     
    espmartin, Jul 31, 2007 IP
  4. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #4
    Thanks, that done it :)

    espmartin:

    $full = nl2br(cleanfunction($_POST['full']));

    mysql_query("INSERT INTO articles (title, short, full, time) VALUES ('" . $title ."', '" . $short . "', '" . $full . "', '" . $time . "')")
    or die(mysql_error());
     
    crazyryan, Jul 31, 2007 IP
  5. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #5
    Actually, I'm having a problem with this conflicting with my cleanfunction.

    
       function cleanstring($string) 
        {        
            if(get_magic_quotes_gpc()) 
            {
                $string = stripslashes($string); 
            }
            elseif(!get_magic_quotes_gpc()) 
            {
                $string = addslashes($string); 
            } 
    		$string = strip_tags($string);
                        
            $string = @mysql_real_escape_string($string);
            return $string; 
        } 
    PHP:
    nl2br(cleanstring($_POST['full']));
    PHP:
    However, when posting:
    Hello

    Hello

    Hello

    The script actually outputs:
    HellornrnHellornrnHello..
     
    crazyryan, Jul 31, 2007 IP
  6. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #6
    It looks like the stripslashes( is messing up your input.

    Since a line break looks like a \r or \n, the strip slashes is removing the \ and you end up with a r or n. nl2br looks for the \n \r and cant find them so it doesn't do anything.
     
    jestep, Jul 31, 2007 IP
  7. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #7
    Ohh, is there any way around that then?
     
    crazyryan, Jul 31, 2007 IP
  8. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #8
    You could add the <br /> before you do anything else with that specific input.

    cleanstring(nl2br($_POST['full']));
     
    jestep, Jul 31, 2007 IP
  9. alhen

    alhen Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    worth telling you about though.

    
    $test=$_POST['test'];
    $test = preg_replace("/\n/","\n<BR>\n",$test);
    
    Code (markup):
    I'm a newb and I'm sure lotsa people can poke holes in this, but there it is.

    Also, I may be TOTALLY missing the point on this, but it seems like the answer to your question may have been complicated. the simple way to use nl2br would be similar to my above code:
    
    $test=$_POST['test'];
    $test = nl2br($test);
    
    Code (markup):
    --
    ~alhen
     
    alhen, Jul 31, 2007 IP
  10. toby

    toby Notable Member

    Messages:
    6,923
    Likes Received:
    269
    Best Answers:
    0
    Trophy Points:
    285
    #10
    hehe, yes, use nl2br . php is great! remember to strip away the tag as well
     
    toby, Jul 31, 2007 IP
  11. feri_soft

    feri_soft Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Well i would suggest you using the nl2br after querying the database not putting in the <br /> 's in the database. This will be more flexible for you to just use the /n as its better supported in php than the html tag. However its your choice .
     
    feri_soft, Jul 31, 2007 IP
  12. nagasharmi

    nagasharmi Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    echo '<br>'.$_POST['text'];
     
    nagasharmi, Aug 2, 2007 IP