使用\\\<br/>,但不创建数组,用新行解除textarea内容 [英] Exploding textarea contents by new line using \n but array not being created

查看:93
本文介绍了使用\\\<br/>,但不创建数组,用新行解除textarea内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 \\\
尝试使用新行展开textarea的内容。但是,数组并没有被创建,它只是将它们合并在一起。我试过让他们的逗号分隔,它的工作正常, \\\
不是我想要使用吗?

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?

textarea包含以下内容:

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);

    }

}


推荐答案

您需要使用双引号将换行符定义为字符:

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'));

这篇关于使用\\\<br/>,但不创建数组,用新行解除textarea内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆