doupo6967 2017-02-24 15:50
浏览 99
已采纳

PHP结果数据溢出/爆炸,带有赋值

I am new to PHP and working to get some results but failing to achieve my target. I have text file which contains data like this,

APAC|AU|enable|SYD1925|8|20150929|WORKING
APAC|AU|disable|ADL7235|3|20120123|RESIGNED
APAC|NZ|disable|NZ1356|6|20110123|RESIGNED
APAC|NZ|enable|NZ1356|3|20130123|WORKING

I am trying to search "AU" && "enable" for this text, line by line and I am a bit successful in it. Here is my Code example;

public function scan1()
{
    $file = FCPATH.'uploads/example.txt';
    // the following line prevents the browser from parsing this as HTML.
    header('Content-Type: text/plain');
    $search1 = "AU"; 
    $search2 = "enable";
    $lines = file($file); 
    foreach($lines as $line) 
    { 
        if(stristr($line,$search1) && stristr($line,$search2))
            echo $line;
    } 
}

Now, I am trying to explode/split output data and assign variable / array to save in database but I am failing to do so, can someone please help or give me some direction to achieve this. Thank you

  • 写回答

3条回答 默认 最新

  • dongshi1880 2017-02-24 16:01
    关注

    Please show us your_table scheme. If '$db' is a handle of the db connection :

    foreach($lines as $line) 
    { 
        if(stristr($line,$search1) && stristr($line,$search2))
        {
            $arr = explode("|", $line);
            $query = "INSERT INTO your_table VALUES ('".$arr[0]."', '".$arr[1]."', '".$arr[2]."', '".$arr[3]."', '".$arr[4]."', '".$arr[5]."')";
            $db->query($query);
        }
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • doujiunai2169 2017-02-24 15:58
    关注

    First, obtain the file contents with file_get_contents:

    $str = file_get_contents(FCPATH.'uploads/example.txt');
    

    Then, use the regex (preg_match_all) to find all portion of text you're looking for:

    preg_match_all("/APAC\\|(\w{2}\\|\w+)/", $str, $matches);
    

    Then adapt the array so the 'AU' and 'enabled' are separated (array_map, explode):

    $matches = array_map(function ($v) { return explode('|', $v); }, $matches[1]);
    

    So, print_r($matches); returns:

    Array
    (
        [0] => Array
            (
                [0] => AU
                [1] => enable
            )
    
        [1] => Array
            (
                [0] => AU
                [1] => disable
            )
    
        [2] => Array
            (
                [0] => NZ
                [1] => disable
            )
    
        [3] => Array
            (
                [0] => NZ
                [1] => enable
            )
    
    )
    

    Finally, the foreach loop:

    foreach($matches as $k => $kv)
    {
        $search1 = $kv[0]; // AU
        $search2 = $kv[1]; // enabled
    }
    
    评论
  • drby30217 2017-02-24 16:27
    关注

    This works as expected:

    public function scan1()
    {
        $file = FCPATH.'uploads/example.txt';
        // the following line prevents the browser from parsing this as HTML.
        header('Content-Type: text/plain');
        $search1 = "AU"; 
        $search2 = "enable";
        $lines = file($file); 
        foreach($lines as $line) 
        { 
            if(strpos($line,$search1) !== false && strpos($line,$search2) !== false)
                echo $line;
        } 
    }
    

    using strpos function for detection.

    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 windows server 2022 datacenter安全策略配置 没有权限问题
  • ¥20 寻找dspace有偿技术支持
  • ¥30 深度学习的模型融合问题
  • ¥20 数电实验Verilog编程
  • ¥15 机器人MATLAB仿真示教占内存过大
  • ¥15 JSR233已经使用但是仍无法将csv数据放到一个list里显示出来,如何解决?
  • ¥15 I350 Gigabit Network
  • ¥15 关于#abap#的问题,请各位专家解答!
  • ¥20 内网通过公网访问外网问题
  • ¥20 谁有这个东西 继续教育的