Motive
Motive

Reputation: 3111

Exploding textarea contents by new line using \n but array not being created

I am using \n to try and explode the contents of a textarea by new line. However, the array is not being created and it's just lumping them together. I've tried making them commas separated and it works fine, is \n not what I want to be using?

The textarea contains something like:

QWERTY
ASDFGH
ZXCVBN

I need each line to become it's own row in the database. I am using the following code to create the array and insert into the database:

public function update_giveaway($item_id) {

    // create array from comma separated string
    $post_keys = explode('\n', $this->input->post('giveaway_keys'));

    $db_keys = $this->get_giveaway_keys($item_id);

    // add tags to database that are in new array, but not in old array
    // returns array
    $add_keys = array_diff($post_keys, $db_keys);

    foreach($add_keys as $key) {

        // add to giveaway table
        $data = array(
          'giveaway_id' => $item_id,
          'key' => $key,
        );

    $this->db->set('date_created', 'NOW()', FALSE);
    $this->db->insert('atk_giveaways', $data);

    }

}

Upvotes: 0

Views: 1160

Answers (1)

Austin
Austin

Reputation: 6034

You need to use double quotes to define newlines as characters:

"\n"

Single quotes notate strings, double quotes notate characters

$post_keys = explode("\n", $this->input->post('giveaway_keys'));

Upvotes: 4

Related Questions