当前位置:Gxlcms > PHP教程 > 文本文件数据库处理函数库_PHP

文本文件数据库处理函数库_PHP

时间:2021-07-01 10:21:17 帮助过:9人阅读

因为我租不起MySQL支持 所以只能采用扁平文件存储

我是采用perl常用的
"|"定界的格式就像 "name|email|homepage" 这样的格式。

随便写了几个函数
觉得还是有点用处的。 大家有什么心得

也希望能告诉我啊
我来完善完善。






// dtf.inc.php3

//

// DTF -- Delimited Text File Functions

// Written by: Goghs

// 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
$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
$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;

}



?&g

人气教程排行