PHP文本的讀寫

來源:互聯網
上載者:User

標籤:die   文本   val   cond   new   關閉   bst   verify   檔案   

 1 <?php 2     $txtPart="test0.txt";    //export 3     $txtPartContent=fopen($txtPart,"r");    //讀檔案,返回TRUE,FALSE 4     if($txtPartContent){    //若檔案存在繼續 5         while(!feof($txtPartContent)){    //feof函數檢查是否已到達 "end-of-file" (EOF),遍曆文本 6             $txtPartContentLine=fgets($txtPartContent);    //fgets() 函數用於從檔案讀取單行,檔案指標會移動到下一行 7             $key=mb_substr($txtPartContentLine,0,17);    //mb_substr(A,B,C)    C:中文是兩個位元組 8             $myArr[$key]=$txtPartContentLine;    //迴圈產生數組 9         }10         fclose($txtPartContent);  //關閉讀檔案11     }12 13     $txtWrite="write0.txt";14     $txtWriteContent=fopen($txtWrite,"w");    //開啟寫檔案15 16     $i=0;17     $txtMain="test1.txt";    //tempor18     $txtMainContent=fopen($txtMain,"r");19     if ($txtMainContent){20         while(!feof($txtMainContent)){21             $isVerify=false;    //預設不是核實人22             $txtMainContentLine=fgets($txtMainContent);23             $keyCompare=mb_substr($txtMainContentLine,0,17);24             foreach($myArr as $x=>$x_value){    //迴圈遍曆數組25                 if($keyCompare==$x){        //如果前17個位元組一樣,輸出數組那行,(數組被匹配)26                     echo $x_value;27                     fwrite($txtWriteContent,$x_value);    //寫入被匹配的數組值28                     $i=$i+1;    //記錄匹配到的次數29                     $isVerify=true;    //如果匹配成功則確認為核實人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 ?>

讀檔案:

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

寫檔案:

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

中文截取:mb_substr()
mb_substr( $str, $start, $length, $encoding )
$str,需要截斷的字串
$start,截斷開始處,起始處為0
$length,要截取的字數
$encoding,網頁編碼,如utf-8,GB2312,GBK

(PS:mb_substr第四個參數編碼格式我沒有加,不知道加完以後是怎麼樣)

測試:

$txtTest="我只是一個測試,今天是2017-02-19,-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/>";

輸出:

我只是一個
我只是一個測試,今天?
是一個測試,今天是20

分析:

$txt2後面出現一個亂碼,難道是因為中文下的","只佔一個位元組,然後另一個中文輸出了一半?

再次測試:

$txtTest="我只是一個測試哈今天是哈2017-02-19,-Mazey";

$txt2=mb_substr($txtTest,0,10,‘utf-8‘);

echo $txt2."<br/>";

輸出:

我只是一個測試哈今天?

分析:

編碼參數換成"GBK"後可以正常顯示,可能含中文的話就要用"GBK",也可能與代碼編碼格式有關,後續遇到問題或者問問其他同事吧。

今天先睡覺了。:)

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

PHP文本的讀寫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.