SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: removing \n

  1. #1
    SitePoint Member
    Join Date
    Dec 2002
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    removing \n

    hi ...

    i know the function nl2br

    but i want all the \n to be removed from a text in a variable... how do i do that ?


    thanks ...

  2. #2
    SitePoint Wizard Mincer's Avatar
    Join Date
    Mar 2001
    Location
    London | UK
    Posts
    1,140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hows about :

    PHP Code:
    <?php
     
    $foo 
    str_replace"\n"""$bar ) ;
     
    ?>
    Matt.

  3. #3
    SitePoint Member
    Join Date
    Dec 2002
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not working
    it comes with line-breaks ...

  4. #4
    SitePoint Wizard Mincer's Avatar
    Join Date
    Mar 2001
    Location
    London | UK
    Posts
    1,140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Which method? The one firepages posted doesn't work because he used single quotes.

    (I saw it mate, don't think you can delete it and hide. )

    The method I posted does work cos I tried it;

    PHP Code:
    <?php
     
    $bar 
    "Hello, this is a \ntwo line string" ;
     
    $foo str_replace"\n"""$bar ) ;
     
    echo 
    "<pre>" ;
    print_r$foo ) ;
    echo 
    "</pre>" ;
     
    ?>
    Matt.

  5. #5
    SitePoint Member
    Join Date
    Dec 2002
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i actually want to put the $var in a javascript and it needds to be in one line but str_replace doesn't remove the line-breaks ...

    e.g.

    $var1 = "Hi
    there
    how are you ";

    So when i output in html it shud come:

    hi there how are you

  6. #6
    SitePoint Wizard Mincer's Avatar
    Join Date
    Mar 2001
    Location
    London | UK
    Posts
    1,140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You must be using an editor set for win32 line breaks. Try this:

    PHP Code:
    <?php
     
    $foo 
    str_replace"\r\n"" "$bar ) ;
     
    ?>
    *note the space in the replace field.

    Matt.

  7. #7
    SitePoint Member
    Join Date
    Dec 2002
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    gr8 this works ... thanx

  8. #8
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol Mincer - I deleted mine cos I saw yours and thought whats the point as I thought they were the same (I started my post 1/2 an hour before I actually pressed submit (no it didnt take me that long to work it out ))

    And if I used single quotes then you are right it would not have worked ! - but hey it was only in existance for a few minutes


Bookmarks

Posting Permissions

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