Character Substitution

Discussion in 'PHP' started by saurabhk, Mar 21, 2006.

  1. #1
    Hi everyone

    A simple question(for most of you):

    A code in a guestbook form:

    <textarea name="comm" rows="7" cols="30">
    </textarea>

    On Server Side in PHP

    <?

    //Some Code.........

    $tbrcom=$_POST['comm'];

    // More Code for storing in database.............

    ?>


    How can I substitute /n or /r characters in "$tbrcom" with "<BR>" for output in HTML?

    Thanks
    Saurabh
     
    saurabhk, Mar 21, 2006 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    T0PS3O, Mar 21, 2006 IP
  3. chengfu

    chengfu Well-Known Member

    Messages:
    113
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #3
    I would recommend deleting all \r and then using nl2br to convert \n to <br>:

    
    $tbrcom=nl2br(str_replace("\r", "", $tbrcom));
    
    Code (markup):
     
    chengfu, Mar 21, 2006 IP
  4. saurabhk

    saurabhk Peon

    Messages:
    149
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks to both of you.
     
    saurabhk, Mar 22, 2006 IP
  5. vishwaa

    vishwaa Well-Known Member

    Messages:
    271
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    138
    #5
    this code will replace single or multiple '\r' and '\n' to single '<br />'
    $tbrcom = preg_replace('/([\r\n])+/', '<br />', $_POST['comm']);
    PHP:
     
    vishwaa, Mar 23, 2006 IP