获取textarea值,用新行分隔,保存到数据库php

时间:2014-05-23 08:37:28

标签: php html database textarea

我的html中有一个名为add-list的textarea。我想获得每个换行符的textarea值,然后将其保存到我的数据库中。问题是,当它将输入保存到数据库时,第二个和后续条目在输入之前有一个空格。

这是获取值的函数:

public function add(){
        $checker = false;
        $names = $this->input->post('add-list');

            if (strpos($names, "\n") == TRUE ) { //newline found
                $names = nl2br(trim($this->input->post('add-list')));
                $namesArray = explode('<br />', $names);

                foreach($namesArray as $name) {
                    $checker = false;
                    $checker = $this->checkDatabase($name); //check if name already exists in database
                    if ($checker) {
                        echo "<script type='text/javascript'>alert('A site in your list already exists. Duplicate sites are not allowed.');</script>";
                    }

                    if (!$checker) {
                        $this->data_model->addCommunity($name);
                    }
                }

                $this->index();
                redirect(base_url());
            }

            else if (strpos($names, "\n") == FALSE) {
                $checker = $this->checkDatabase($names);
                if ($checker) {
                    echo "<script type='text/javascript'>alert('" . $names . " already exists. Duplicate sites are not allowed.'); window.location.href='".base_url()."'</script>";
                }

                if (!$checker) {
                    $this->data_model->addCommunity($names);
                    $this->index();
                    redirect(base_url());
                }
            }
    }

我在数据库中得到的是这样的:

firstName
 secondName //there's a whitespace before 's'

请帮帮我!!!

1 个答案:

答案 0 :(得分:1)

为什么你一直在通过nl2br然后爆炸而不是使用带有换行符的爆炸?但只需使用搜索或搜索引擎,例如Explode PHP string by new line(很长时间没有PHP,所以我可能不太正确)。