Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHPExcel-我的CSV自动检测程序:我可以在原始上传文件之前访问它吗';解析了什么?_Php_Csv_Phpexcel - Fatal编程技术网

PHPExcel-我的CSV自动检测程序:我可以在原始上传文件之前访问它吗';解析了什么?

PHPExcel-我的CSV自动检测程序:我可以在原始上传文件之前访问它吗';解析了什么?,php,csv,phpexcel,Php,Csv,Phpexcel,我希望在这里找到一些PHPExcel用户,因为他们讨论组中的活动似乎有点悠闲:)。(我在那里的原始帖子) 我拼凑了一个函数,用于自动检测CSV文件的分隔符和/或附件。 现在它正在运行,我想将其插入PHPExcel(通过扩展CSV类)。 我唯一的问题是我的OOP技能还很年轻,我在寻找如何/在何处集成它时遇到了一些困难 我的函数目前采用通过file()创建的数组,但如果需要,我可以轻松更改该数组 function autoDetect(array $file, array $toDetect=arr

我希望在这里找到一些PHPExcel用户,因为他们讨论组中的活动似乎有点悠闲:)。(我在那里的原始帖子)

我拼凑了一个函数,用于自动检测CSV文件的分隔符和/或附件。
现在它正在运行,我想将其插入PHPExcel(通过扩展CSV类)。
我唯一的问题是我的OOP技能还很年轻,我在寻找如何/在何处集成它时遇到了一些困难

我的函数目前采用通过
file()
创建的数组,但如果需要,我可以轻松更改该数组

function autoDetect(array $file, array $toDetect=array(true,false), $sampleSize=5){  

    $detectDelim = $toDetect[0]? true: false;
    $detectEncl = $toDetect[1]? true: false; 
    $sampleSize = ( count($file) < $sampleSize)? count($file): $sampleSize;  // set sample-size to the lesser value    
    array_splice($file, $sampleSize);  // trim down the array to only first X rows

    $delimiters = array(',','^','.',';',':',"\t"); // first elem will be the dflt
    $delimRegex = implode('',$delimiters);

    $enclosures = array('"',"'",'^'); // first elem will be the dflt
    $enclRegex = implode('',$enclosures);

    foreach ($file as $row) {
        $row=preg_replace( '/\r\n/', '', trim($row) );  // clean up .. strip new line and line return chars

        if($detectDelim){
            $stripped=preg_replace( "/[^$delimRegex]/", '', $row);  // clean up .. strip evthg x'ept dilim's
            $delimRowChars = str_split($stripped);  // get each char so we can inspect individually
            $delimCount = _count_instances($delimRowChars, $delimiters);  // TODO : fix how this overwrites itself
            // TODO : set delim
        }

        if($detectEncl){
            $stripped=preg_replace( "/[^$enclRegex]/", '', $row);  // clean up .. strip evthg x'ept dilim's
            $enclRowChars = str_split($stripped);  // get each char so we can inspect individually
            $enclCount = _count_instances($enclRowChars, $enclosures);  // TODO : fix how this overwrites itself
            // TODO : set encl
        }
    }

    echo'<pre>delims found in sample set: ', print_r($delimCount), '</pre>';  // For Testing ---->
    echo'<pre>encls found in sample set: ', print_r($enclCount), '</pre>';  // For Testing ---->
    echo "<pre>Suggested Delimiter: '",_array_max($delimCount),"' </pre>";  // For Testing ---->    
    echo "<pre>Suggested Enclosure: '",_array_max($enclCount),"' </pre>";  // For Testing ---->

    //return TODO ;        
}


/**
 * 
 */
function _count_instances(array $haystacks, array $needles, $maxOnly = false){
    $basket = array();  // instantiate
    foreach ($haystacks as $haystack) {
        foreach ($needles as $needle) {  // this throws an undef'd index err and adds an element to the array
            if( strpos($haystack, $needle) !== false) {  // if the needle is in the haystack ...
                if($needle == "\t") $needle = '\t';  // TODO : decouple this from "\t" so it can work for other chars too
                $basket[$needle]++;  // ... increment
            }
        }
    }
    if($maxOnly) $basket = _array_max($basket);
    return $basket;
}

/**
 * 
 */
function _array_max(array $target){
    $target = array_keys($target, max($target));
    $target = $target[0];
    return $target;
}
函数自动检测(数组$file,数组$toDetect=array(true,false),$sampleSize=5){
$detectDelim=$toDetect[0]?真:假;
$detectEncl=$toDetect[1]?真:假;
$sampleSize=(计数($file)<$sampleSize)?计数($file):$sampleSize;//将样本大小设置为较小的值
array_splice($file,$sampleSize);//将数组缩小到仅前X行
$delimiters=array(',','^',',';',':',“\t”);//第一个元素将是dflt
$delimRegex=内爆(“”,$delimiters);
$enclosures=array(“,“,”,“^”);//第一个元素将是dflt
$enclRegex=内爆(“”,$enclosures);
foreach($file as$row){
$row=preg_replace('/\r\n/','',trim($row));//清理..剥离新行和行返回字符
如果($detectDelim){
$stripped=preg_replace(“/[^$delimRegex]/”,“,$row);//清理..剥去evthg x'ept dilim的
$delimRowChars=str_split($stripped);//获取每个字符,以便我们可以单独检查
$delimCount=\u count\u实例($delimRowChars,$delimiters);//TODO:修复它如何覆盖自身
//TODO:设置delim
}
如果($detectEncl){
$stripped=preg_replace(“/[^$enclRegex]/”,“,$row);//清理..剥离evthg x'ept dilim的
$enclRowChars=str_split($stripped);//获取每个字符,以便我们可以单独检查
$enclCount=\u count\u实例($enclRowChars,$enclosures);//TODO:修复这将如何覆盖自身
//TODO:设置encl
}
}
echo'delims found in sample set:',print_r($delimCount),'';//用于测试-->
echo“在样本集中找到encls:”,print\r($enclCount),“”;//用于测试-->
echo“建议的分隔符:'”,_array_max($delimCount),“'”;//用于测试-->
echo“建议的存储模块:”,_array_max($enclCount),“”;//用于测试-->
//返回待办事项;
}
/**
* 
*/
函数_count_实例(数组$haystacks,数组$pinees,$maxOnly=false){
$basket=array();//实例化
foreach($haystacks as$haystack){
foreach($pineels as$pineel){//这会抛出一个未定义的索引错误,并向数组中添加一个元素
如果(strpos($haystack,$needle)!==false){//如果针在haystack中。。。
if($pinder==“\t”)$pinder='\t';//TODO:将其与“\t”解耦,以便它也可以用于其他字符
$basket[$needle]++;/…增量
}
}
}
如果($maxOnly)$basket=_数组_max($basket);
返回$basket;
}
/**
* 
*/
函数\u数组\u最大值(数组$target){
$target=数组_键($target,max($target));
$target=$target[0];
返回$target;
}
在解析文件之前,我只需要运行自动检测器,但在对象中看不到信息。
何时/何地/如何接通电源?
是否可以访问原始文件,或者???

PHPExcel社区最终提供了一个响应,因此为了子孙后代,我想我应该把它放在这里:)
也可以通过OP顶部提供的链接看到

“您需要将呼叫发送到自动检测器中 加载到PHPExcel/Reader/CSV.php文件的现有方法中;但是 脚本一次读取一行CSV,而不是加载每一行 进入内存(我们有足够的内存问题,没有刻意尝试 创建它们)。从逻辑上讲,您可能只想加载一些 检查BOM表后,立即设置 $this->\u分隔符值,然后记得倒带文件 后来。”


希望它能帮助其他人。

PHPExcel社区最终提供了一个响应,所以我想为了子孙后代,我应该把它放在这里:)
也可以通过OP顶部提供的链接看到

“您需要将呼叫发送到自动检测器中 加载到PHPExcel/Reader/CSV.php文件的现有方法中;但是 脚本一次读取一行CSV,而不是加载每一行 进入内存(我们有足够的内存问题,没有刻意尝试 创建它们)。从逻辑上讲,您可能只想加载一些 检查BOM表后,立即设置 $this->\u分隔符值,然后记得倒带文件 后来。”

希望它能帮助别人