Results 1 to 9 of 9

Thread: Splitting Text

  1. #1
    Registered Mike's Avatar
    Join Date
    May 2003
    Location
    UK
    Posts
    2,755

    Splitting Text

    Hi all,

    I've got yet another question for you all, this time regarding splitting text with PHP.

    I've got a database, with fields full of paragraphs. Each paragraph is seperated with a blank line, no html tag or anything.

    The problem with this is when I edit it it all comes out in one chunk. I need each paragraph to be outputted like it is in the database. I've tried replacing /n with <p> in an explode function, but that doesn't seem work, so does anyone have any ideas?

    Cheers,
    Mike
    Don't you just love free internet games ?

  2. #2
    Administrator Chris's Avatar
    Join Date
    Feb 2003
    Location
    East Lansing, MI USA
    Posts
    7,055
    you did it wrong

    its \n not /n
    Chris Beasley - My Guide to Building a Successful Website[size=1]
    Content Sites: ABCDFGHIJKLMNOP|Forums: ABCD EF|Ecommerce: Swords Knives

  3. #3
    Registered Mike's Avatar
    Join Date
    May 2003
    Location
    UK
    Posts
    2,755
    Yeah think I tried that as well, still doesn't seem to work though.

    PHP Code:
    $split explode("\n""<p>"$allrow[content]);
    echo 
    $split[0]; 
    That's what I have, anyone know what is wrong?

    Thanks
    Don't you just love free internet games ?

  4. #4
    Registered Mike's Avatar
    Join Date
    May 2003
    Location
    UK
    Posts
    2,755
    Hey, why am I using explode?

    Just done basically the same as above apart from with str_replace and it works.

    Don't have a clue why I used explode originally, heh, problem solved
    Don't you just love free internet games ?

  5. #5
    Your syntax for explode is wrong.. you are using 2 params, should only be one:

    Code:
     $split = explode("\n", "<p>", $allrow[content]);
    echo $split[0];
    Should be:

    Code:
     $split = explode("\n", $allrow[content]);
    echo $split[0];
    I think that's where you went off book

  6. #6
    Registered Mike's Avatar
    Join Date
    May 2003
    Location
    UK
    Posts
    2,755
    Yeah haha

    I got the two completely mixed up, hehe...
    Don't you just love free internet games ?

  7. #7
    Registered The New Guy's Avatar
    Join Date
    May 2004
    Posts
    283
    You might find this useful

    PHP Code:
        function nl2p ($text){
            
    $text '<p>' $text '</p>';
            
    $text str_replace("\n",'</p><p>',$text);
            
    $text str_replace("\r",'',$text);
            
    $text str_replace('<p></p>','',$text);
            
    $text str_replace('</p><p>'"</p>\n<p>"$text);

            return 
    $text;
        } 

  8. #8
    TNG - Good function. I like nl2br and this really compliments that.

  9. #9
    Registered Member moonshield's Avatar
    Join Date
    Aug 2004
    Location
    Charlotte
    Posts
    1,281
    I second that. Excellent function, quite useful for many uses.

Similar Threads

  1. Replies: 1
    Last Post: 06-19-2005, 09:41 PM
  2. Replies: 0
    Last Post: 06-05-2005, 09:33 PM
  3. Replies: 1
    Last Post: 05-31-2005, 05:58 AM
  4. Replies: 0
    Last Post: 05-01-2005, 07:35 PM
  5. osCommerce text editing
    By Stevens in forum Website Programming & Databases
    Replies: 6
    Last Post: 01-26-2005, 08:32 AM

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
  •