在CodeIgniter Active Record中使用空格搜索

时间:2016-01-21 14:47:04

标签: php codeigniter

我创建了一个用于搜索用户的函数。但是......我需要在名称之间用空格搜索它。

例如,搜索'John Doe':

我的网址应为:

  

https://localhost/cp/admin/users/get/John Doe

我不知道该怎么做,但是,我得到空响应,因为他无法识别空格。

代码(我不知道这可能会有什么帮助,但确定。):

public function get($username) {
    $res = $this->db->where("username", $username)->get("users");
    $res = array("user" => $res->row());
    echo json_encode($res, JSON_UNESCAPED_UNICODE);
}

1 个答案:

答案 0 :(得分:0)

你不能传递这样的url参数。

在添加到控制器中的网址和urlencode()之前,您需要urldecode()

urlencode()将所有非安全字符替换为安全字符。 urldecode恰恰相反。

对于您的示例,解码后,John Doe在网址中变为John+Doe,控制器中变为John Doe