SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Oct 2002
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    removing line breaks

    Hi,

    I am converting a record into a CSV file but am having a problem with the data stored in the database that has text spread over different lines ie :

    14 Drewberry Terrace
    Town
    County
    PE0 3KD

    I want to remove these lines so that they go into 1 CSV cell, but don't seem to be able to remove them.

    I've tried the following to replace the line break with a '|' but this doesn't seem to work.

    $tcell=str_replace("
    "," | ","$tcell");


    Any ideas?

    Thanks
    Eternity

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Whistler, Canada
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $tcell=str_replace("
    "
    ," | ","$tcell"); 
    A newline is "\n" (mind double quotes). Also have a look here.

    Hope this helps.

    Cheers,
    Pepe

  3. #3
    SitePoint Addict
    Join Date
    Oct 2002
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This doesn't seem to work. The breaks are put in the database record. They are not <br />. Looking at the CSV file it displays the new line symbol (the square).
    Eternity

  4. #4
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This works on my machine:
    PHP Code:
    <?php
    $string 
    "14 Drewberry Terrace
    Town
    County
    PE0 3KD"
    ;
    $newstring implode(" "explode("\n"$string));
    echo 
    $newstring;
    ?>

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    $s = "14 Drewberry Terrace\nTown\nCounty\nPE0 3KD";
    // \r\n = Windows; \r = Mac; \n = Unix/Linux;
    $s = str_replace(arra("\r\n", "\r", "\n"), '|', $s);

    Thats not so hard.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    SitePoint Addict
    Join Date
    Oct 2002
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks logic earth - that works.
    Eternity


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
  •