本文实例讲述了php将文本文件转换csv输出的方法。分享给大家供大家参考。具体实现方法如下:
这个类提供了转换成固定宽度的CSV文件,快速,简便的方法,它可将SplFileObject用于执行迭代,使它非常高效的一个迭代只知道当前成员,期权是提供给指定行字符和字段分隔符结束,This from CSV files.这个类是特别有用的,如果数据需要来自一个固定宽度的文件,并插入到数据库中,因为大多数的数据库支持从CSV文件中的数据输入.
这一类的方便的功能是可以跳过字段如果不是在输出需要,该领域的阵列提供,提供了一个键/值对,与主要持有的价值偏移,或启动领域的地位,和值包含的宽度,或字段的长度,For example.例如,12 =“10是一个领域,在12位和宽度或字段的长度为10个字符开始.
底的行字符默认成“ n”,而是可以设置为任何字符。
分隔符默认为一个逗号,但可以设置为任何字符,或字符。
从文件的输出可以直接使用,写入一个文件,到数据库或任何其他目的插入.
PHP实例代码如下:
复制代码 代码如下:<"Unable to set $name " );
}
}
/**
*
* Gettor This is called when trying to access a non-existing property
*
* @access public
* @param string $name The name of the property
* @throw Exception if proplerty cannot be set
* @return string
*
*/
public function __get ( $name )
{
switch( $name )
{
case 'eol' :
return " " ;
case 'fields' :
return array();
case 'separator' :
return ',' ;
default:
throw new Exception ( " $name cannot be set" );
}
}
/**
*
* Over ride the parent current method and convert the lines
*
* @access public
* @return string The line as a CSV representation of the fixed width line, false otherwise
*
*/
public function current ()
{
if( parent :: current () )
{
$csv = '' ;
$fields = new cachingIterator ( new ArrayIterator ( $this -> fields ) );
foreach( $fields as $f )
{
$csv .= trim ( substr ( parent :: current (), $fields -> key (), $fields -> current () ) );
$csv .= $fields -> hasNext () "codetitle">复制代码 代码如下:<?php
try
{
/*** the fixed width file to convert ***/
$file = new fixed2CSV ( 'my_file.txt' );
/*** The start position=>width of each field ***/
$file -> fields = array( 0 => 10 , 10 => 15 , 25 => 20 , 45 => 25 );
/*** output the converted lines ***/
foreach( $file as $line )
{
echo $line ;
}
/*** a new instance ***/
$new = new fixed2CSV ( 'my_file.txt' );
/*** get only first and third fields ***/
$new -> fields = array( 0 => 10 , 25 => 20 );
/*** output only the first and third fields ***/
foreach( $new as $line )
{
echo $line ;
}
}
catch( Exception $e )
{
echo $e -> getMessage ();
}
?>
希望本文所述对大家的php程序设计有所帮助。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]