Results 1 to 2 of 2

Thread: As what is line breaks stored in the database?

  1. #1

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    As what is line breaks stored in the database?

    Strange how quick one forget things...9 months no PHP and I'm concatenating strings with +, call members with "." instead of "=>"....ai

    Anyhows, question:

    I'll be saving data directly from a textarea to a text field in a mysql database.
    When reading it back, I'll need to split the string on it's line breaks to create list items (<li>).

    Q1: Forgot...as what is linebreaks saved from a textarea to the database? \r\n no?

    Q2: How this look for a class member reading the above back from the database, split it into an array ($this->notes is the text saved earlier to the database...did not create the functionality for that yet):
    Code:
    function getNotes() {
    	$records = preg_split('/[\r\n]+/', $this->notes, -1, PREG_SPLIT_NO_EMPTY);
    	return $records;			
    }
    Above work as is (assuming the line break will be \r\n, I entered some data manually into the database), but when using it, I have to strip off the trailing \r\n. Don't like that.
    Code:
    $notes = $post->getNotes();
    foreach($notes as $note) {
    	echo('<li>'.str_replace('\r\n', '', $note).'</li>');
    }
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: As what is line breaks stored in the database?

    1. line breaks saved via a form are usually \r\n, yes. however, sometimes I'll split using \n and replace all of the \r.

    2. the function looks fine to me! like I said above, the only thing I might think about changing is stripping all of the returns (\r) and splitting with new line (\n) instead -- at least, as long as you're running into the problem you described.

Posting Permissions

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



Click Here to Expand Forum to Full Width