Author: redfox mail: ask4more@163.net & nbsp; home page: netnote.oso.com.cn I believe that everyone on the Internet to apply for free php space, if it is a beginner user, generally did not have MySQL available for use, one of our solutions to data processing is to use text files. But what method can be used to process text data as quickly and conveniently? In my experience, I think the following Author: redfox mail: ask4more@163.net
Home: http://netnote.oso.com.cn
I believe that the free php space applied for on the internet is generally unavailable for beginners, so one of our solutions to data processing is to use text files. But what method can be used to process text data as quickly and conveniently?
In my experience, I think the file structure in the following columns is optimal:
----------------------------------------------------------------------
File extension:. php
Email = ask4more@13.net & nickname = redfox & realname = Ding & url = http://netnote.oso.com.cn &...
...
----------------------------------------------------------------------
As you may see, the extension with. php is used, and the first line of the file is This effectively blocks unauthorized access to data files. The format of the second row of the file is: variable name 1 = value 1 & variable name 2 = value 2 &...
It is very easy to put forward all the variables, that is, to use the parse_str () function ();
For example:
$ Theline = "email = ask4more@13.net & nickname = redfox & realname = Ding & url = http://netnote.oso.com.cn ";
Parse_str ($ theline); // extract the variable $ email, $ nickname, $ realname, $ url
Echo "I am $ nickname, my real name is $ realname
";
Echo "welcome to visit my website: $ url
";
Echo "email me at: $ email ";
?>
Running result:
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, this article stipulates that 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 row. Well, with such a file structure can easily implement GuestBook, BBS, or even the community's data processing :) my home page "network notes" http://netnote.oso.com.cn, is achieved in this way.
In order to facilitate the majority of users, I have compiled several functions, and I will give the necessary explanations below. Of course, you can modify and modify it as needed, but you must ensure the functional integrity. Save the following code as textfun. inc (of course, the same is true for other names), and add a line of statement at the beginning of the file you want to use. Then you can use the function I have compiled for you.
Below we have a total of db objects, one 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, which is also available in 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;"); // To Get environment variables
Eval ("\ $ therow = \" $ therow \";");
$ Fp = fopen ($ this-> $ dbfile, "");
Fputs ($ fp, "$ therow \ n ");
Fclose ($ fp );
}
Function readall ($ f ){
If (file_exists ($ f )){
$ This-> $ dbfile = $ f;
$ Rows = file ($ f );
For ($ I = 1; $ I $ Temp [] = $ rows [$ I];
}
Return $ temp;
}
}
// Read all data rows in reverse order
Function revread ($ f ){
If (file_exists ($ f )){
$ 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;
$ J --;
}
}
For ($ I = 0; $ I $ Temp [] = $ rows [$ I];
}
Return $ temp;
}
}
Function close (){
$ This = $ nothing;
}
}
// Format the paragraph text into a line of text for easy storage
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 is the data object defined in this document. It includes six methods: createdb (), opendb (), insertline (), readall (). revread (), close ();
Db-> createdb (string filename)
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)
Example: Include ("textfun. inc ");
$ Mydb = new db;
$ Mydb-> opendb ("UserInfo. php ");
?>
This method "opens" The data file UserInfo. php in append mode. If this file does not exist, it is created.
Therefore, this method can replace the createdb () method. (But never delete the createdb () function in class db {}: P)
Db-> insertline (string VarString)
Example: Include ("textfun. inc ");
$ Theline = "email = ask4more@13.net & nickname = redfox & realname = Ding & url = http://netnote.oso.com.cn ";
Parse_str ($ theline); // Construct an environment variable
$ Mydb = new db;
$ Mydb-> opendb ("UserInfo. php ");
$ Mydb-> insertline ("nickname | realname | email | url ");
?>
Db-> insertline () can separate corresponding environment variables from strings such as "nickname | realname | email | url" and store them in the form agreed in this article. To input the insertline () parameter, you must use "|" to concatenate the environment variable name into a string. there are no restrictions on the number, but do not add "$" in front. well, is a string like "nickname | realname | email | url :~)
Array db-> readall (string filename)
Example: Include ("textfun. inc ");
$ Mydb = new db;
$ Allrec = $ mydb-> readall ("UserInfo. php ");
?>
The readall () method returns ( ). Each row corresponds to an element of the array.
Array db-> revread (string filename)
Example: Include ("textfun. inc ");
$ Mydb = new db;
$ Allrec = $ mydb-> revread ("UserInfo. php ");
?>
The revread () method reads the first line ( ), Returns an array. This is especially useful when we compile the message book.
Void db-> close ()
Disable db objects.
Now we can use the db object to compile a simple message book.
--------- Guestbook. php ------------
My message book
Include ("textfun. inc ");
If ($ Submit ){
$ Thetime = date ("Y-m-d h: m: s ");
$ Message = p2row ($ message );
$ Mydb = new db;
$ Mydb-> opendb ("msg. php ");
$ Mydb-> insertline ("nickname | email | url | message | thetime ");
// Read all the data below
$ Allrecs = $ mydb-> revread ("msg. php ");
While (list ($ key, $ theline) = each ($ allrecs )){
Parse_str ($ theline );
?>
">
URL: ">
Message:
}
$ Mydb-> close ();
}
?>
-----------------------------
Well, although this message book is not very beautiful, it is mainly used to illustrate the usage of db objects ~ :)
This article passes debugging under WIN98 + PWS + PHP4!