PHP text read/write,

Source: Internet
Author: User

PHP text read/write,

1 <? Php 2 $ txtPart = "test0.txt"; // export 3 $ txtPartContent = fopen ($ txtPart, "r"); // read the file, return TRUE, FALSE 4 if ($ txtPartContent) {// if the file exists, continue 5 while (! Feof ($ txtPartContent) {// feof Function check if "end-of-file" (EOF) has been reached, traverse text 6 $ txtPartContentLine = fgets ($ txtPartContent ); // The fgets () function is used to read A single row from A file. The file pointer will be moved to the next row 7 $ key = mb_substr ($ txtPartContentLine,); // mb_substr (A, B, C) c: two bytes in Chinese: 8 $ myArr [$ key] = $ txtPartContentLine; // cyclically generated array 9} 10 fclose ($ txtPartContent ); // close Read File 11} 12 13 $ txtWrite = "write0.txt"; 14 $ txtWriteContent = fopen ($ txtWrite, "w "); // open the Write File 15 16 $ I = 0; 17 $ txt Main = "test1.txt"; // tempor18 $ txtMainContent = fopen ($ txtMain, "r"); 19 if ($ txtMainContent) {20 while (! Feof ($ txtMainContent) {21 $ isVerify = false; // It is not verified by default by 22 $ txtMainContentLine = fgets ($ txtMainContent); 23 $ keyCompare = mb_substr ($ txtMainContentLine, ); 24 foreach ($ myArr as $ x => $ x_value) {// looping array 25 if ($ keyCompare = $ x) {// if the first 17 bytes are the same, output the row of the array (the array is matched) 26 echo $ x_value; 27 fwrite ($ txtWriteContent, $ x_value ); // write the matched array value 28 $ I = $ I + 1; // record the matching times 29 $ isVerify = true; // if the match is successful, it is confirmed to be verified by 30} 31} 32 if ($ isVerify = false) {33 Echo $ txtMainContentLine; 34 fwrite ($ txtWriteContent, $ txtMainContentLine); 35} 36 echo "<br/>"; 37} 38 fclose ($ txtMainContent ); 39} 40 41 fclose ($ txtWriteContent); 42 43 echo "Yep! ". $ I; 44?>

Read files:

$ Myfile = fopen ("webdictionary.txt", "r") or die ("Unable to open file! ");
Echo fread ($ myfile, filesize ("webdictionary.txt "));
Fclose ($ myfile );

Write File:

$ Myfile = fopen ("newfile.txt", "w") or die ("Unable to open file! ");
$ Txt = "First Line ";
Fwrite ($ myfile, $ txt );
$ Txt = "Second Line ";
Fwrite ($ myfile, $ txt );
Fclose ($ myfile );

Interception: mb_substr ()
Mb_substr ($ str, $ start, $ length, $ encoding)
$ Str: string to be truncated
$ Start: truncates the start point. the start point is 0.
$ Length, number of words to be intercepted
$ Encoding: webpage encoding, such as UTF-8, GB2312, and GBK

(PS: I didn't add the fourth parameter encoding format for mb_substr. I don't know what it will do after it is added)

Test:

$ TxtTest = "I'm just a test. Today is,-Mazey ";

$ Txt1 = mb_substr ($ txtTest, 0, 10 );

$ Txt2 = mb_substr ($ txtTest, 0, 10, 'utf-8 ');

$ Txt3 = mb_substr ($ txtTest, 2, 10, 'utf-8 ');

Echo $ txt1. "<br/> ";

Echo $ txt2. "<br/> ";

Echo $ txt3. "<br/> ";

Output:

I am just
I'm just a test, today?
Is a test, today is 20

Analysis:

$ Txt2 is followed by a garbled code. Is it because "," in Chinese only occupies one byte, And the other Chinese outputs half of it?

Test again:

$ TxtTest = "I'm just a test. Today is Ha,-Mazey ";

$ Txt2 = mb_substr ($ txtTest, 0, 10, 'utf-8 ');

Echo $ txt2. "<br/> ";

Output:

I'm just a test. Why today?

Analysis:

After the encoding parameter is changed to "GBK", it can be displayed normally. If the encoding parameter contains Chinese characters, "GBK" may be used, or it may be related to the code encoding format. If you encounter any problems later, ask other colleagues.

Today, I went to bed first. :)

$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");echo fread($myfile,filesize("webdictionary.txt"));fclose($myfile);

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.