Home  >  Article  >  Backend Development  >  Using text for data processing_PHP tutorial

Using text for data processing_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:07:21739browse

Author: redfox Email: ask4more@163.net
Homepage: http://netnote.oso.com.cn

I believe that everyone who applies for free PHP space online, if they are junior users, usually If MySQL is not available, one of our solutions to data processing is to use text files. But what method can be used to process text data fastest and most conveniently?
Based on my experience, I think the following file structure is optimal:
------------------------------- ------------------------------------------
File extension: . php

email=ask4more@13.net & nickname=redfox & realname=ADING& url=http://NetNote.oso.com .cn & ...
...
---------------------------------- ----------------------------------
Maybe everyone has noticed that it is extended with .php name, and the first line of the file is , which effectively prevents illegal access to the data file. The format of the second line of the file is: Variable name 1 = value 1 & Variable name 2 = value 2 &...
Proposing all variables is very simple, just use the function parse_str();
For example:
$theline="email=ask4more@13.net&nickname=redfox&realname=A Ding&url=http://NetNote.oso.com.cn";
parse_str($theline);// Separate the variables $email, $nickname, $realname, $url
echo "I am $nickname,my real name is $realname
";
echo "welcome to visit my website:$urlecho "email me at:$email";
?>
Running results:
I am redfox,my real name is A Ding
welcome to visit my website: http://NetNote.oso.com.cn
email me at:ask4more@13.net  

Therefore, according to this article, the data text structure is:
-------- --------------------------------

Variable name 1 = value 1 & variable name 2 = value 2 & ...

File extension: .php
--------------- -----------------------------

The real data starts from the second line. Well, using such a file structure, you can easily implement GuestBook, BBS, and even community data processing:) My homepage "Netnote" http://netnote.oso.com.cn is implemented in this way .
In order to facilitate the majority of netizens, I have compiled several functions, and the necessary explanations will be given below. Of course you can modify and implement it as you like, but you must ensure the integrity of the functionality. Please save the following code as textfun.inc (of course it is the same with other names), and add a line of statement at the beginning of the file you want to use, you You can use the function I compiled for you.
There is a db object below, a function p2row();

--------------textfun.inc------------- ---
class db{
var $dbfile;
function createdb($dbName){
$f=$dbName;
$this->$ dbfile=$f;
$headInfo="n";
$fp=fopen($f,"w");
fputs ($fp,$headInfo);
fclose($fp);
chmod($f,0777);//Modify the file mode, also available under Unix
return(1);
}
function opendb($f){
$this->$dbfile=$f;
if(file_exists($f)){
return true;
}else{
$this->createdb($f);
}
}
function insertline($info){
$fields=explode("|",$info);
while(list($key,$val)=each($fields)){
$therow.="$val=$".$val."&";
$var1.="$" .$val.",";
}
$var1.='$tail';
eval("global $var1;"); //In order to obtain the environment variable
eval("$ therow="$therow";");
$fp=fopen($this->$dbfile,"a");
fputs($fp,"$therow");
fclose( $fp);
}
function readall($f){
if(file_exists($f)){
$this->$dbfile=$f;
$rows= file($f);
for($i=1;$i $temp[]=$rows[$i];
}
       return $temp;
$this->$dbfile=$f;
$rows=file($f);
$d=count($rows);
$j=$d-1;
for($i=0;$i<$d;$i++){
if($i<$j){
$temprow=$rows[$i];
$rows[ $i]=$rows[$j];
           $rows[$j]=$temprow;                                 ; $i $temp[]=$rows[$i];
}
return $temp;
}
}

function close(){
$this=$nothing;
}
}

//Format the paragraph text into one line of text for convenience Store
function p2row($t){
$t=nl2br(stripslashes(htmlspecialchars($t)));
for($i=0;$i $c=substr($t,$i,1);
if(ord($c)==10) $c=" ";
$tempstr.=$c;
}
return $tempstr;
}
?>
————————————————————————————————————————————————————

db It is our customized data object for this article, including six methods: createdb(), opendb(), insertline(), readall().revread(), close();

db->createdb( string filename)
Usage example: include("textfun.inc");
$mydb=new db;
$mydb->createdb("UserInfo.php");
?>
This method creates a file UserInfo.php, the first line is

db->opendb(string filename)
Usage example: include("textfun.inc");
$mydb=new db;
$mydb->opendb("UserInfo.php");
?>
This method "opens" the data file UserInfo.php in append mode. If the file does not exist, it will be created.
Therefore, this method can replace the createdb() method. (But don’t delete the createdb() function in class db{ } :P)

db->insertline(string VarString)
Usage example: include( "textfun.inc");
$theline="email=ask4more@13.net&nickname=redfox&realname=A Ding&url=http://NetNote.oso.com.cn";
parse_str($theline); //Construct environment variables
$mydb=new db;
$mydb->opendb("UserInfo.php");
$mydb->insertline("nickname|realname|email|url" ; document. When passing in the parameters of insertline(), you must use "|" to connect the environment variable names into a string. There is no limit to the number, but do not add "$" in front. Well, it must be in the form of "nickname|realname" |email|url" such a string :~)

array db->readall(string filename)
Usage example: include("textfun.inc");
$mydb=new db;
$allrec=$mydb->readall("UserInfo.php");
?>
readall() method returns the first row except the first row (), with each row corresponding to an element of the array.

array db->revread(string filename)
Usage example: include("textfun.inc");
$mydb=new db;
$ allrec=$mydb->revread("UserInfo.php");
?>
revread() method reads in reverse order except the first line (), returns an array. This is especially useful when we are writing guest books etc.

void db->close()
Close the db object.

Okay, now we will use the db object to compile the simplest guestbook.
---------guestbook.php------------
我的留言本


>
NickName:

E-Mail:

Homepage:

Message:




include("textfun.inc");
if($Submit){
  $thetime=date("Y-m-d h:m:s A");
  $message=p2row($message);
  $mydb=new db;
  $mydb->opendb("msg.php");
  $mydb->insertline("nickname|email|url|message|thetime");

  //以下读出所有的数据
  $allrecs=$mydb->revread("msg.php");
  while(list($key,$theline)=each($allrecs)){
    parse_str($theline);
    ?>
    

    URL:

    Message:


      }
  $mydb->close();
}
?>
-----------------------------
好了,虽然这个留言本不是很美观,但主要是为了举例说明db对象的用法~:)
本文在WIN98+PWS+PHP4下调试通过!

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/315137.htmlTechArticle作者:redfox邮件:ask4more@163.net 主页:http://netnote.oso.com.cn 相信大家在网上申请的免费PHP空间,如果是初级用户,一般都是没得MySQL可供使用...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn