The best replace for me

Discussion in 'PHP' started by onlyican.com, Jan 7, 2006.

  1. #1
    hey

    What i got is a form, using <textarea>
    The form then goes to a MySQL db, as a text field

    Then that field is echoed to the website somewhere else.

    BUT

    as you may know using a form
    You Type
    we're going to the pub tonight
    You want to come.

    It will come out
    we/'re going to the pub tonight\nYou want to come

    but for the text echoed from the db, it needs to be <br> not \n

    WHats the best way to stripshlashes AND replace \n with <br>
     
    onlyican.com, Jan 7, 2006 IP
  2. cytech

    cytech Guest

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can do this:

    $newstring=VALUE_FROM_DB;

    $newstring=str_replace("\n","<BR>",$newstring);
    $newstring=stripslashes($newstring);
     
    cytech, Jan 7, 2006 IP
  3. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    str_replace is the best,

    I aint done replace yet, so I didn;t know what was the best

    As it goes, I need slashes for the db anyway, and it is automaticly stripped on exit.
     
    onlyican.com, Jan 8, 2006 IP
  4. TMan

    TMan Peon

    Messages:
    126
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Why use str_replace? Php has the function nl2br I believe, which is made solely for this purpose so probably best.
     
    TMan, Jan 8, 2006 IP
  5. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    So, back to my question

    What is "better coding"

    str_replace or
    nl2br
     
    onlyican.com, Jan 8, 2006 IP
  6. TMan

    TMan Peon

    Messages:
    126
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I would recommend nl2br, it's far more obvious what your code does that way. Opinions may vary however.
     
    TMan, Jan 9, 2006 IP
  7. cytech

    cytech Guest

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I think it depends on how large the string is. If the string is quite large it is probably best to use preg_replace. I have never used nl2br but Reading up on it - thats what its made to do. So I say go with it :)
     
    cytech, Jan 9, 2006 IP
  8. Djohn

    Djohn Peon

    Messages:
    75
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I always use nl2br and I fail to see how using alternate solutions as opposed to an existing function - for that very purpose - would be "better coding".
     
    Djohn, Jan 9, 2006 IP
  9. cytech

    cytech Guest

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    well, "better coding" comes down to speed correct?

    So, preg_replace has proven itself to be the fastest search replace with large files and strings. BUT if you are working with small strings nl2br is great. :D
     
    cytech, Jan 10, 2006 IP
  10. TMan

    TMan Peon

    Messages:
    126
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I believe better coding to come down to readability, but I guess it depends where your priorities are. :cool:
     
    TMan, Jan 10, 2006 IP
  11. cytech

    cytech Guest

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Ahh, but isn't that why we have comments :D
     
    cytech, Jan 10, 2006 IP