php将文本文件转换csv输出的方法

本文实例讲述了php将文本文件转换csv输出的方法。分享给大家供大家参考。具体实现方法如下:

这个类提供了转换成固定宽度的CSV文件,快速,简便的方法,它可将SplFileObject用于执行迭代,使它非常高效的一个迭代只知道当前成员,期权是提供给指定行字符和字段分隔符结束,This from CSV files.这个类是特别有用的,如果数据需要来自一个固定宽度的文件,并插入到数据库中,因为大多数的数据库支持从CSV文件中的数据输入.

这一类的方便的功能是可以跳过字段如果不是在输出需要,该领域的阵列提供,提供了一个键/值对,与主要持有的价值偏移,或启动领域的地位,和值包含的宽度,或字段的长度,For example.例如,12 =“10是一个领域,在12位和宽度或字段的长度为10个字符开始.

底的行字符默认成“ n”,而是可以设置为任何字符。

分隔符默认为一个逗号,但可以设置为任何字符,或字符。

从文件的输出可以直接使用,写入一个文件,到数据库或任何其他目的插入.

PHP实例代码如下:

代码如下:

<?php
/** 
* Class to convert fixed width files into CSV format 
* Allows to set fields, separator, and end-of-line character 

* @author Kevin Waterson 
* @url http://phpro.org 
* @version $Id$ 

*/ 
class fixed2CSV extends SplFileObject 

/** 

* Constructor, duh, calls the parent constructor 

* @access       public 
* @param    string  The full path to the file to be converted 

*/ 
public function __construct ( $filename ) 

parent :: __construct ( $filename ); 
}
 
/* 
* Settor, is called when trying to assign a value to non-existing property 

* @access    public 
* @param    string    $name    The name of the property to set 
* @param    mixed    $value    The value of the property 
* @throw    Excption if property is not able to be set 

*/ 
public function __set ( $name , $value ) 

switch( $name ) 

case 'eol' : 
case 'fields' : 
case 'separator' : 
$this -> $name = $value ; 
break;
 
default: 
throw new Exception ( "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 () ? $this -> separator : $this -> eol ; 

return $csv ; 

return false ; 

} // end of class
?>

Example Usage示例用法

代码如下:

<?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程序设计有所帮助。

(0)

相关推荐

  • 使用PHP+JavaScript将HTML页面转换为图片的实例分享

    1,准备要素 1)替换字体的js文件 js代码: function com_stewartspeak_replacement() { /* Dynamic Heading Generator By Stewart Rosenberger http://www.stewartspeak.com/headings/ This script searches through a web page for specific or general elements and replaces them wi

  • 分享php代码将360浏览器导出的favdb的sqlite数据库文件转换为html

    下面给大家分享了一段php代码,讲解将360浏览器导出的favdb的sqlite数据库文件转换为html,下面代码简单易懂,感兴趣的朋友看一下吧. php代码如下所示: <?php $book_mark_name = 'book_mark.html'; $content = file_get_contents('tb_fav.json'); var_dump($content); $content_list = json_decode($content,'utf-8'); $content_li

  • PHP将HTML转换成文本的实现代码

    核心代码: <?php // $document 应包含一个 HTML 文档. // 本例将去掉 HTML 标记,javascript 代码 // 和空白字符.还会将一些通用的 // HTML 实体转换成相应的文本. $search = array ("'<script[^>]*?>.*?</script>'si", // 去掉 javascript "'<[\/\!]*?[^<>]*?>'si", //

  • PHP实现把文本中的URL转换为链接的auolink()函数分享

    其实我在<把文本中的URL地址转换为可点击链接的JavaScript.PHP自定义函数>一文中介绍过PHP代码如何实现将URL地址转化成链接的方法,今天给大家介绍一个更加简洁的版本,先来看看PHP的源代码: auolink() API 复制代码 代码如下: /** * Author: SeeDZ * From: http://code.seebz.net/p/autolink-php/ **/ function autolink($str, $attributes = array()) {  

  • 把文本中的URL地址转换为可点击链接的JavaScript、PHP自定义函数

    这几天在写一个小程序的时候,需要用到正则表达式匹配用户输入文本中的URL地址,然后将URL地址替换成可以点击的链接.URL地址的匹配,我想这应该是大家在做验证处理中常会用到的,这里就把我整合的一个比较完整的表达式给出来: 复制代码 代码如下: var URL = /(https?:\/\/|ftps?:\/\/)?((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(:[0-9]+)?|(localhost)(:[0-9]+)?|([\w]+\.)(\S+)(\w{2,4}

  • php中将html中的br换行符转换为文本输入中的换行符

    下面这几个方法将能够帮你解决这个问题. PHP版将html中的<br />换行符转换为文本框中的换行符: 复制代码 代码如下: function br2nl($text){    return preg_replace('/<br\\s*?\/??>/i','',$text);} 或者: 复制代码 代码如下: function br2nl($text){    $text=preg_replace('/<br\\s*?\/??>/i',chr(13),$text); r

  • php实现二进制和文本相互转换的方法

    本文实例讲述了php实现二进制和文本相互转换的方法.分享给大家供大家参考.具体如下: 这段代码包含两个函数,bin2text,二进制转换为文本,text2bin,文本转换成二进制 <?php function bin2text($bin_str) { $text_str = ''; $chars = explode("\n",chunk_split(str_replace("\n",'',$bin_str),8)); $_I = count($chars);

  • php导出csv格式数据并将数字转换成文本的思路以及代码分享

    php导出csv格式数据实现:先定义一个字符串 存储内容,例如 $exportdata = '规则111,规则222,审222,规222,服2222,规则1,规则2,规则3,匹配字符,设置时间,有效期'."\n"; 然后对需要保存csv的数组进行foreach循环,例如 复制代码 代码如下: if (!empty($lists)){                  foreach($lists as $key => $value){                    $ti

  • php实现转换html格式为文本格式的方法

    本文实例讲述了php实现转换html格式为文本格式的方法.分享给大家供大家参考,具体如下: 有时候需要转换html格式的字符串为文本,但又需要保持一定的格式,比如要求段落变成的分段格式就可以用下面这个函数 function html2text($str){ $str = preg_replace("/<style .*?<\\/style>/is", "", $str); $str = preg_replace("/<script

  • php实现使用正则将文本中的网址转换成链接标签

    复制代码 代码如下: function text2links($str='') {     if($str=='' or !preg_match('/(http|www\.|@)/i', $str)) { return $str; }     $lines = explode("\n", $str); $new_text = '';     while (list($k,$l) = each($lines)) {         // replace links:         $l

  • 自动把纯文本转换成Web页面的php代码

    首先让我们来看一个我朋友希望转换的纯文本文件的例子: 以下为引用的内容: 复制代码 代码如下: Green for Mars! John R. Doe The idea of little green men from Mars, long a staple of science fiction, may soon turn out to be less fantasy and more fact. Recent samples sent by the latest Mars explorati

随机推荐