我希望在这里找到一些PHPExcel用户,因为他们讨论组中的活动似乎有点悠闲:)。(我在那里的发帖,can be seen here)
我拼凑了一个用于自动检测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;
}在文件被解析之前,我只需要运行我的自动检测器,并且我看不到对象中的信息。
它什么时候/在哪里/怎么插上电源?是否可以访问原始文件,或者?
发布于 2012-07-12 19:43:48
PHPExcel社区最终确实提供了一个响应,所以我想为了子孙后代的缘故,我应该在这里对它进行修改:)
它也可以通过向OP顶部提供的链接来查看。
“您需要将对此自动检测器的调用放在PHPExcel/Reader/CSV.php文件的loadIntoExisting方法中;但是脚本一次只读取一行CSV,而不是将每一行都加载到内存中(我们没有刻意创建内存问题就有足够的内存问题)。从逻辑上讲,您可能希望在检查BOM之后立即加载几行代码,设置$ this ->_delimiter值,然后记住随后对文件进行回滚。”
希望它能帮到别人。
https://stackoverflow.com/questions/11456456
复制相似问题