douzi1986 2018-11-19 02:54
浏览 85
已采纳

如何将数据库中的ip地址值拆分为CodeIgniter中的数组格式

I'm working on the CodeIgniter application where I want to split IP address into the array format. Actually, I'm getting values from the database column but unable to create the array from the dynamic values which are getting from the database?

Controller Code

Getting Values from the database 

$system = $this->Xin_model->read_enter_allow_ip_address_info(1);
$allowlist =  array($system[0]->enter_allow_ip_address);
$finalipallow = nl2br($allowlist);

if(!in_array($_SERVER['REMOTE_ADDR'],$finalipallow)){
$Return['error'] = 'Login Failed';  
$this->output($Return); 
}

Models Query

public function read_enter_allow_ip_address_info($id) {

    $condition = "setting_id =" . "'" . $id . "'";
    $this->db->select('enter_allow_ip_address');
    $this->db->from('xin_system_setting');
    $this->db->where($condition);
    $this->db->limit(1);
    $query = $this->db->get();      
    return $query->result();
}

Dynamic Values From The Database Column

'10.0.0.1','10.0.0.2','10.0.0.3','::1'

how do I split IP address values from database into the array format like this static array

$finalipallow = array(
        '10.0.0.1',
        '10.0.0.2',
        '10.0.0.3',
        '::1'
);

I have a static array which is working with if condition but I want to convert dynamic values into the array like the static array?

Working now here the solution of it

$system = $this->Xin_model->read_enter_allow_ip_address_info(1);
$allowlist =  $system[0]->enter_allow_ip_address;

$finalipallow = str_getcsv($allowlist, ",", "'");

if(!in_array($_SERVER['REMOTE_ADDR'],$finalipallow)){
   $Return['error'] = 'Login Failed';   
   $this->output($Return);  
}

展开全部

  • 写回答

2条回答 默认 最新

  • duangouhui0446 2018-11-19 02:58
    关注

    You can use str_getcsv() to process it as a CSV record...

    $data = "'10.0.0.1','10.0.0.2','10.0.0.3','::1'";
    $finalipallow = str_getcsv($data, ",", "'");
    
    print_r($finalipallow);
    

    which outputs...

    Array
    (
        [0] => 10.0.0.1
        [1] => 10.0.0.2
        [2] => 10.0.0.3
        [3] => ::1
    )
    

    Just change $data to the value from the database. Using the deimter and the optional enclosed parameters allows it to strip off the extra quotes from the data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 在更新角色衣服索引后,Sprite 并未正确显示更新的效果该如何去解决orz(标签-c#)
  • ¥15 VAE代码如何画混淆矩阵
  • ¥15 求遗传算法GAMS代码
  • ¥15 雄安新区高光谱数据集的下载网址打不开
  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥100 求一个c#通过CH341读取数据的Demo,能够读取指定地址值的功能
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部