php - 对单行使用 2 个定界符展开

标签 php mysql import

我需要在数据库中上传一个 .txt 文件。 我的 .txt 文件看起来完全像

Name|Code|Email|Designation|Number|Salary|Age\t
syed|101|syed@gmail.com|trainee|7222877798|6000|21\t
hari|102|hari@gmail.com|trainee|9554512582|6000|23\t 

我需要用 |\t 将它分开。

在获取数组时,第一个达到了我的预期。但我不能让 \t 爆炸.. 任何人都可以在这个论坛上帮助我吗?? 我的例程如下所述

   if ($_POST['frmSubmit']) {
            $file = $_FILES['frmUpload']['tmp_name'];           //  Get Temporary filename
                $handle = fopen($file,"r");                         //  Open the file and read
                while($strBookData = fgets($handle, 4096)) {        //  To get Array from .txt
                    $strDatas[] = $strBookData;
                    $strTableColumn = count($strBookData);  

                }
                $strDatas = explode("|",implode($strDatas));

                printArray($strDatas); exit;

                if ($strDatas) {
                    $strInsertRecords = 0;
                    $strDuplicationRecords = 0;
                    if ($strTableColumn == 7) {
                        for($k=1; $k<count($strDatas); $k++) { //$k=1 is initialized because $k[0] is a header field array.
                            $strStatus  =   doCheckDuplication($strDatas[$k]['2']);                 
                            if ($strStatus == 0) {
                                // Insert Code  
                                $strData = $strDatas[$k];
                                doInsertEmployeeDetails($strData['0'], $strData['1'], $strDatas[$k]['2'], $strData['3'], $strData['4'], $strData['5'], $strData['6']);
                                $strInsertRecords++;            // To Get Inserted Records Count.
                            } else {
                                $strDuplicationRecords++;       // To Get Duplication Records Count.
                            }
                        }
                }
    }

最佳答案

您好,这将拆分您提供的文本。

    $text = 'Name|Code|Email|Designation|Number|Salary|Age\t
    syed|101|syed@gmail.com|trainee|7222877798|6000|21\t
    hari|102|hari@gmail.com|trainee|9554512582|6000|23\t';

    //remove line endings
    $text = str_replace(array("\r\n", "\r", "\n"), "", $text);

    $rows = explode('\t', $text);
    $data = array();
    foreach ($rows as $row){
            //don't include empty lines
        if(!empty( $row )){
            $data[] = explode('|', $row);
        }
    }
    echo '<pre>';
    var_export( $data );

输出:

array (
            0 =>
            array (
                    0 => 'Name',
                    1 => 'Code',
                    2 => 'Email',
                    3 => 'Designation',
                    4 => 'Number',
                    5 => 'Salary',
                    6 => 'Age',
            ),
            1 =>
            array (
                    0 => 'syed',
                    1 => '101',
                    2 => 'syed@gmail.com',
                    3 => 'trainee',
                    4 => '7222877798',
                    5 => '6000',
                    6 => '21',
            ),
            2 =>
            array (
                    0 => 'hari',
                    1 => '102',
                    2 => 'hari@gmail.com',
                    3 => 'trainee',
                    4 => '9554512582',
                    5 => '6000',
                    6 => '23',
            ),
    );

然而,您的示例中发生了很多事情,例如读取文件。如果文件不是很大,最好的办法是使用 file_get_contents() 来读取文件一次完成整个文件。否则在这部分

   $handle = fopen($file,"r");                         //  Open the file and read
   while($strBookData = fgets($handle, 4096)) {        //  To get Array from
        $strDatas[] = $strBookData;
        $strTableColumn = count($strBookData);  
   }

您最好只连接文本。

   $strDatas = '';
   $handle = fopen($file,"r");                         //  Open the file and read
   while($strBookData = fgets($handle, 4096)) {        //  To get Array from
        $strDatas .= $strBookData; 
   }

然后像我上面那样 split 。

关于php - 对单行使用 2 个定界符展开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25429315/

相关文章:

javascript - 如何调用 php 变量作为 jQuery 变量?

c++ - 将矩阵从 C++ 移动到 Matlab

MongoDB fatal error : runtime: out of memory

将 Maya ASCII 导入游戏

php - 在php中使用if else语句插入数据库表

Javascript 打开 xml 但不执行函数内的任务

php - Kohana 3.3 包罗万象的路线

按多列分组时,MySQL 查询隐藏结果

java - 在搜索栏中使用单引号

php - 记录不会从数据库中删除