当前位置:首页> PHP教程> php基础
关键字
文章内容
文本文件数据库处理函数库
 
 
修改时间:[2010/03/29 09:57]    阅读次数:[838]    发表者:[起缘]
 
因为我租不起MySQL支持, 所以只能采用扁平文件存储,
我是采用perl常用的"|"定界的格式,就像 "name|email|homepage" 这样的格式。
随便写了几个函数, 觉得还是有点用处的。 大家有什么心得,
也希望能告诉我啊, 我来完善完善。
<?php
// dtf.inc.php3
//
// DTF -- Delimited Text File Functions
// Written by: Goghs <goghs@mail.com>
// http://www.eqiao.com
//
// Function to manage a delimited text file database
function dtf_fetch_all_as_string($filename) {
$fp = fopen( $filename, "r" );
$current = fread($fp, filesize($filename));
fclose($fp);
return $current;
}
function dtf_fetch_all_as_array($dbname) {
return file($dbname);
}
function dtf_update_db($dbname, $item) {
if (file_exists($dbname)):
$fp = fopen($dbname,"w+");
fputs($fp,$item);
fclose($fp);
else:
$fp = fopen($dbname,"w");
fputs($fp,$item);
fclose($fp);
endif;
}
function dtf_get_total_rows($dbname) {
return count(file($dbname));
}
function dtf_remove_pipe($input) {
if (is_string($input)) {
// $input=ereg_replace("|","",$input); // It’s said that str-replace is faster than ereg_replace
$input=str_replace("|","",$input);
}
if (is_array($input)) {
for ($i=0; $i<count($input); $i++) {
$input[$i] = ereg_replace("|","|",$input[$i]);
}
}
return $input;
}
// 这个函数没有用, 用htmlspecialchars就可以
// 但是如果只需要处理"<",">"而不需要处理 引号和 &时就很有用
function dtf_remove_html_tag($input) {
if (is_string($input)) {
$input = ereg_replace("<","<",$input);
$input = ereg_replace(">",">",$input);
}
if (is_array($input)) {
for ($i=0; $i<count($input); $i++) {
$input[$i] = ereg_replace("<","<",$input[$i]);
$input[$i] = ereg_replace(">",">",$input[$i]);
}
}
return $input;
}
// 可以定制这个函数, 控制是否允许html标记等
function dtf_sanitize($input) {
$input = dtf_remove_pipe($input);
// $input = dtf_remove_html_tag($input);
$input = stripslashes($input);
$input = htmlspecialchars($input);
return $input;
}
?&gBy phpfans.net收集整理