Using text for data processing _php tutorial

Source: Internet
Author: User
Author: Redfox Mail: ask4more@163.net
Home: http://netnote.oso.com.cn

I believe that everyone in the online application of free PHP space, if it is a novice user, generally do not have MySQL to use, then we solve the data processing method is a text file. But in what way can the fastest and most convenient processing of text data?
In my experience, I think the following document structure is the best:
----------------------------------------------------------------------
File extension:. php

Email=ask4more@13.net & Nickname=redfox & realname= Ardin & url=http://netnote.oso.com.cn & ...
...
----------------------------------------------------------------------
Maybe we all see it, with. PHP to do the extension, and the first line of the file is , which effectively prevents unauthorized access to the data file. The second line of the file is in the format: variable name 1= value 1 & variable name 2= value 2 & ...
It is simple to propose all the variables, that is, to use the function parse_str ();
For example:
$theline = "Email=ask4more@13.net&nickname=redfox&realname= Ardin &url=http://netnote.oso.com.cn";
Parse_str ($theline);//separate variables, $nickname, $realname, $url
echo "I am $nickname, my real name is $realname
";
echo "Welcome to visit my website: $url
";
echo "Email me at: $email";
?>
Operation Result:
I am redfox,my real name is Ardin
Welcome to visit my website:http://netnote.oso.com.cn
Email me at:ask4more@13.net

Therefore, in 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, with such a file structure can be easily implemented Guestbook,bbs, even the community of data processing: My home page "network Notes" http://netnote.oso.com.cn, this is the implementation.
In order to facilitate the vast number of netizens, I made a few functions, the following will make the necessary explanations. Of course you can modify and carry the shellfish casually, but you must ensure the integrity of the function. Save the following code as TEXTFUN.INC (with the same name, of course), adding a line to the beginning of the file you want to use , you can use the function I made for you.
Below is a total of one DB object, 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. " & ";
$var 1.= "\$". $val. ",";
}
$var 1.= ' $tail ';
Eval ("Global $var 1;"); To get the environment variable
Eval ("\ $therow =\" $therow \ ";");
$FP =fopen ($this, $dbfile, "a");
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 paragraph text into a single 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 our custom data object for this document, 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, and the first line is

DB-&GT;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, and if the file does not exist, it is 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= Ardin &url=http://netnote.oso.com.cn";
Parse_str ($theline);//Tectonic environment variable
$mydb =new DB;
$mydb->opendb ("userinfo.php");
$mydb->insertline ("Nickname|realname|email|url");
?>
Db->insertline () can separate the corresponding environment variable into a string that is shaped like "nickname|realname|email|url", and deposit the file in the form agreed to in this article. To pass in the parameters of the Insertline (), be sure to use the "|" The environment variable name string, the number of unlimited, but do not add "$" in front of Oh, ah, is to shape such as "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");
?>
The ReadAll () method returns except for the first row ( An array of all the data, each 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");
?>
The Revread () method reads in reverse order except for the first line ( ), and returns an array of all data. This is especially useful when we are in the message book.

void Db->close ()
Close the DB object.

Well, we're going to use the DB object to make one of the simplest message books.
---------guestbook.php------------
My message Book



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");

read out 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 is not very beautiful, but mainly to illustrate the use of DB objects ~:)
This article under the WIN98+PWS+PHP4 debugging passes!

http://www.bkjia.com/PHPjc/315137.html www.bkjia.com true http://www.bkjia.com/PHPjc/315137.html techarticle Author: Redfox e-mail: ask4more@163.net home: http://netnote.oso.com.cn believe that everyone in the online application of free PHP space, if it is a novice user, generally do not have MySQL to use ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.