How can I add <br/> tags to data gotten from a text box?

Discussion in 'PHP' started by Imozeb, Jun 10, 2010.

  1. #1
    How can I add <br/> tags to data gotten from a text box and are there any special characters I should delete (characters which people normally use to hack databases and urls)?
     
    Imozeb, Jun 10, 2010 IP
  2. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if it's a large textbox, nl2br() the text.

    DO NOT write your own queries - use prepared statements/stored procedures
     
    krsix, Jun 10, 2010 IP
  3. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Right!
    if you need to demolish the things that hacks DB
    Put on the other side to match and replace that and after Process and Execute the Query
     
    roopajyothi, Jun 11, 2010 IP
  4. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the help.
     
    Imozeb, Jun 11, 2010 IP
  5. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I tested it and it isn't working.

    My Test Code:
    
    $var = "Test\nText";
    $var = nl2br($var, true);  //This statement says that I have an incorrect amount of parameters in the command
    echo($var);
    
    Code (markup):
    I need the <br/> tags to be strict compliant. (Not <br>. Needs to be <br/>)

    I basically need a function which will replace all newlines (When the user presses the 'Enter' Key) from a textbox with <br/> tags so it can be outputed as html. I also need a function which will replace all second spaces which &nbsp; so it can also be printed out as html. Is there a function which will do all this quickly?

    Thank you.
     
    Imozeb, Jun 11, 2010 IP
  6. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    edit:


    Don't use anything:

    nl2br(text) = <br /> by default on all modern versions of PHP
     
    Last edited: Jun 11, 2010
    krsix, Jun 11, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    Read the documentation if no second paremeter (is_xhtml) is postively defined it will use the xhtml br tag by default, theirfore if it is (which you have done) it will display the non-xhtml-valid br tag.

    Change:

    $var = nl2br($var, true);
    PHP:
    Too:

    $var = nl2br($var);
    PHP:
     
    danx10, Jun 11, 2010 IP
  8. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Okay. What about this part of my question.

    I also need a function which will replace all second spaces with &nbsp; so it can also be printed out as html. Is there a function which will do all this quickly?
     
    Imozeb, Jun 11, 2010 IP
  9. laces12

    laces12 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Yes.
    
    $var = str_replace(' ','&nbsp;',$var);
    
    Code (markup):
     
    laces12, Jun 11, 2010 IP