一定数量的字符后的结束行

时间:2016-10-24 13:34:04

标签: php arrays parsing text-files

我有一个包含大量插入的文本文件,如下所示:

INSERT INTO yyy VALUES ('1','123123','da,sdadwa','6.7','24f,5','f5,5','dasdad,fsdfsdfsfsasada dasdasd','aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa','dasdasd','q231e','','0','','g','1','123123','dasdadwa','6.7','24f,5','f5,5','dasdad,fsdfsdfsfsasada dasdasd','','','q231e','','0','','a','1','123123','dasdadwa','655.755','24f,5','f5,5','dasdad,fsdfsdfsfsasada dasdasd','','','q231e','','','','a');
INSERT INTO yyy VALUES ('2','123123','dasdadwa','6.8','24f,6','f5,5','dasdad,fsdfsdfsfsasada dasdasd','aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa','dasdasd','q231e','','0','','g','2','123123','dasdadwa','6.8','24f,6','f5,5','dasdad,fsdfsdfsfsasada dasdasd','','','q231e','','0','','a','2','123123','dasdadwa','6.8','24f,6','f5,5','dasdad,fsdfsdfsfsasada dasdasd','','','q231e','','','','a');
INSERT INTO yyy VALUES ('3','123123','dasdadwa','6.9','24f,7','f5,5','dasdad,fsdfsdfsfsasada dasdasd','aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa','dasdasd','q231e','','0','','g','3','123123','dasdadwa','6.9','24f,7','f5,5','dasdad,fsdfsdfsfsasada dasdasd','','','q231e','','0','','a','3','123123','dasdadwa','6.9','24f,7','f5,5','dasdad,fsdfsdfsfsasada dasdasd','','','q231e','','','','a');
INSERT INTO yyy VALUES ('4','123123','dasdadwa','6.10','24f,8','f5,5','dasdad,fsdfsdfsfsasada dasdasd','aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa','dasdasd','q231e','','0','','g','4','123123','dasdadwa','6.10','24f,8','f5,5','dasdad,fsdfsdfsfsasada dasdasd','aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa','','q231e','','0','','a','4','123123','dasdadwa','6.10','24f,8','f5,5','dasdad,fsdfsdfsfsasada dasdasd','aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa','','q231e','','','','a');
INSERT INTO yyy VALUES ('5','123123','dasdadwa','6.11','24f,9','f5,5','dasdad,fsdfsdfsfsasada dasdasd','aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa','dasdasd','q231e','','0','','g','5','123123','dasdadwa','6.11','24f,9','f5,5','dasdad,fsdfsdfsfsasada dasdasd','aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa','','q231e','','0','','a','5','123123','dasdadwa','6.11','24f,9','f5,5','dasdad,fsdfsdfsfsasada dasdasd','aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa','','q231e','','','','a');

我必须修改此文本文件,以便每行最多可包含50个字符。问题是我不能简单地在50个字符后面加上一个结束,因为这会破坏那些插入中的元素,所以我需要将结束行放在最后一个逗号之前。

对于第一行,它需要是这样的:

INSERT INTO yyy VALUES ('1','123123','da,sdadwa',
'6.7','24f,5','f5,5',
'dasdad,fsdfsdfsfsasada dasdasd',
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'dasdasd','q231e','','0','','g','1','123123',
'dasdadwa','6.7','24f,5','f5,5',
'dasdad,fsdfsdfsfsasada dasdasd','','','q231e','',
'0','','a','1','123123','dasdadwa','655.755',
'24f,5','f5,5','dasdad,fsdfsdfsfsasada dasdasd',
'','','q231e','','','','a');

正如你所看到的那样,即使在元素内部也可以使用逗号(' da,sdadwa')这使得这有点困难。我尝试将所有内容放入数组中,但遇到了一些问题并且无法使其工作。 我尝试了什么:

if(is_array($testFileContents))
    {
        foreach($testFileContents as $line)
        {
            $j=0;
            for($i=0;$i<=strlen($line);$i++)
            {
                //echo $line[$i];
                $ct=1;
                if($j==50)
                {
                    if($line[$j]==",")
                    {
                        //$line[$j]=$line[$j].PHP_EOL;
                    }   
                    else
                    {
                        $temporaryJ = $j;
                        while($line[$temporaryJ]!=",")
                        {
                            $temporaryJ--;
                        }
                        //$line[$temporaryJ] = $line[$temporaryJ].PHP_EOL;
                        //$j=$i-$ct*50;
                        $j=0;
                        $ct=$ct+1;
                        echo $ct." ";
                    }

                }
                else
                {
                    $j++;
                }
            }
        }
    }

我知道必须有一种更简单的方法来解决这个问题,而不使用数组,但我无法弄明白。

1 个答案:

答案 0 :(得分:0)

您可以使用preg_split()分割线条。我发现另一个用户在this answer中发布了一个模式,用于匹配INSERT语句的值:     "~'(?:\\\\'|[^'])*'(*SKIP)(*F)|,~"。这利用Special Backtracking Control Verbs

您可以使用this PhpFiddle中的PHP代码。

foreach($lines as $line) {
    $matches = preg_split("~'(?:\\\\'|[^'])*'(*SKIP)(*F)|,~",$line);
    $currentIndex = 0;
    $currentLine = '';
    $outputLines = array();
    $delimeter = ',';
    while($currentIndex < count($matches)) {
        if ($currentIndex == count($matches)-1 ) {
            $delimeter = '';
        }
        $tempLine = $currentLine . $matches[$currentIndex] . $delimeter;
        if (strlen($tempLine) <= 50) {
            $currentLine .= $matches[$currentIndex] . $delimeter;
        }
        else {//push current line into array and start a new line
            $outputLines[] = $currentLine;
            $currentLine = $matches[$currentIndex] . $delimeter;
        }
        if ($currentIndex == count($matches)-1 ) {
            $outputLines[] = $currentLine;
        }
        $currentIndex++;
    }
    //can use implode("\n",$outputLines) to write out to file 
    //or whatever your needs are
}