使用2分隔符分隔单行


Explode using 2 delimiter for a single line

我需要在数据库中上传一个。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; 
   }

然后像上面那样分割

我认为你应该首先使用''t'作为分隔符来获得由't分隔的子字符串(名称|代码|电子邮件|指定|编号|薪水|年龄)然后使用'|'作为分隔符展开每个子字符串。我希望这能帮到你