易截截图软件、单文件、免安装、纯绿色、仅160KB

几个 PHP 文件读写函数和 CSVFileObject 类

/**
* 打开关闭文件
*/
fopen() // 2/4: 2 个必选参数, 4 个参数;
fclose() // 1/1;
/**
* 读取文件
*/
// 需先用 fopen() 打开文件才能读取的函数;从文件指针处开始读取
fgetc() // 1/1; 读取一个字符
fgets() // 1/2; 读取一行
fgetss() // 1/3; 读取一行并去除 HMTL 标签
fgetcsv() // 1/5; 读取一行并解析为 csv 字段,返回数组
fread() // 2/2; 读取指定字节的字符
fscanf() // 2/n; 从文件中格式化输入
// 不需要先 fopen() 打开文件的函数
file() // 1/3; 读取整个文件内容到一个数组,文件中一行对应数组中一个元素
file_get_contents() // 1/5; 读取整个文件到一个字符串
readfile() // 1/3; 读入一个文件并写入到输出缓冲。
/**
* 写入文件
*/
fwrite() / fputs() // 2/3; 写入文件(可安全用于二进制文件), 需先 fopen()
fputcvs() // 2/4; 将行格式化为 CSV 并写入文件指针,需先 fopen()
file_put_contents() // 2/4; 将字符串整个写入文件
/**
* 文件指针
*/
ftell() // 1/1; 返回文件指针读/写的位置
fseek() // 2/3; 在文件指针中定位
rewind() // 1/1; 倒回文件指针的位置
/**
* 使用 SplFileObject 方法 fgetcsv() 时最后一行后面还会解析出一个数组
* eg:
* $it = new SplFileObject('sample.csv');
* while( $arr = $it->fgetcsv() ){
* print_r( $arr)
* }
*/
class CSVFileObjectException extends Exception{
//
}
class CSVFileObject extends SplFileInfo implements Iterator{
protected $current_line;
protected $map;
protected $column_cnt;
protected $fp;
function __construct( $filename, $mode, $use_include_path = FALSE, $context = NULL)
{
$this->ensure( ! is_readable( $filename), "CSV file {$filename} does not exists or is not readable");
if( is_null( $context)){
$this->fp = @fopen( $filename, $mode, $use_include_path);
}else{
$this->fp = @fopen( $filename, $mode, $use_include_path, $context);
}
$this->ensure( ! $this->fp, "open CSV file {$filename} failure");
parent::__construct( $filename);
$this->current_line = 0;
$


相关文档:

PHP header() examples

 //用这个header指令来解决URL重写产生的404 header     
header('HTTP/1.1 200 OK');     
   
// 页面没找到     
header('HTTP/1.1 404 Not Found');     ......

ubuntu 9.10 安装 php mysql apache(LAMP)

 一、安装
1. 首先安装SSH
sudo apt-get install ssh
2.安装MySQL(虽然现在最新版为5.1,但是还只能装5.0版本)
sudo apt-get install mysql-server-5.0
3.安装Apache
sudo apt-get install apache2
4.安装PHP
sudo apt-get install php5 libapache2-mod-php5
5.重启Apache
sudo /etc/init.d/apache2 restart ......

PHP函数:ctype_digit

 (PHP 4 >= 4.0.4)
功能说明:Check for numeric character(s)
Description
bool ctype_digit ( string text)
Returns TRUE if every character in text is a decimal digit, FALSE otherwise.
例子 1. A ctype_digit() example
<?php $strings = array('1820.20', '10002', 'wsl!12'); foreach ($strings ......

PHP 小技巧

=================================
mb_substr($str, 0, 1, "gbk");
如果提供了第四个参数,php会解析这个参数
1. 将这个参数转换成字符串.
2. 调用mbfl_name2no_encoding获得编码器序号
3. 第二步会调用mbfl_name2encoding,这个函数在循环中使用了strcasecmp
strcasecmp是忽略大小写的字符串匹配,性能很低, ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号