【问题标题】:codeigniter multiple spaces into single space将多个空格编码为单个空格
【发布时间】:2016-07-22 14:03:34
【问题描述】:

我是codeigniter的新手,我只是想知道是否有任何方法可以将多个空格转换为一个空格

这是我在控制器/验证部分的代码

    public function main_page_add_enc(){
        $this->load->library('form_validation');

        $this->form_validation->set_rules('add_fname', 'First Name', 'trim|required|min_length[2]|max_length[50]|regex_match[/^[A-Za-z ]+$/]');
        $this->form_validation->set_rules('add_lname', 'Last Name', 'trim|required|min_length[2]|max_length[50]');

        if($this->form_validation->run() == FALSE){
$data['view']=$this->Student_model->stud_view();
        $this->load->view('main_page',$data);
        }
        else{
            $this->load->view('index');
        }
    }

如果我输入: sssssssssssssssssssssssssssssssssssssssss

它会变成: sssssssssssssssssssssssssssssssssssssssss

【问题讨论】:

  • 如果两个或多个单词之间有多个空格,那么您希望将其设为一个空格。这就是你想要的吗?

标签: forms codeigniter validation


【解决方案1】:

首先使用 codeigniter 字符串助手

    $this->load->helper('string');


    $string = reduce_multiples($string," ");//this will be double spaces

http://www.codeigniter.com/user_guide/helpers/string_helper.html

第二个你可以用这个

preg_replace

 $output = preg_replace('!\s+!', ' ', $input);

http://www.regular-expressions.info/reference.html

【讨论】:

    猜你喜欢
    • 2014-05-21
    • 2013-11-25
    • 2016-11-07
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    相关资源
    最近更新 更多