PHP 檔案系統管理函數與 preg_replace() 函數過濾代碼

來源:互聯網
上載者:User

標籤:style   blog   io   color   ar   os   使用   for   sp   

案例:在帶行號的代碼至檔案 crop.js 中。用兩種方法去掉代碼前面的行號,帶行號的程式碼片段:

1.$(function(){2. //初始化圖片地區3. var myimg = new Image(); 4. myimg.src = $("#mypic2").attr("src"); 5. //輸出圖片資料6. $("#showSize").html(myimg.width + "×" + myimg.height);7. 8. //初始化圖片的位置,根據圖片的寬度調整左右9. $("#statistics, #picArea").css("left",$(window).width()/2-myimg.width/2);10. $("#picArea").width(myimg.width).height(myimg.height); 11. var parentWidth = parseInt($("#picArea").width());12. var parentHeight = parseInt($("#picArea").height());13. 14. //顯示滑鼠的相對於圖片的座標(左上方為(0,0))15. var offsetX = parseInt($("#picArea").css("left"));16. var offsetY = parseInt($("#picArea").css("top"));...

 

1. file() 函數

使用 使用 file() 函數,把每一行代碼作為數組參數儲存進一個一維數組:

$file = "crop.js";$arr = file($file);foreach($arr as $key=>$value){        $value = preg_replace("/^[0-9]*./","",$value);        $arr[$key] = $value;}$str = implode("<br>",$arr);echo $str;

直接在頁面上讀出正則匹配過的內容,然後把內容拷貝進檔案

 

2.把正則匹配過的內容儲存至新檔案 

$filename = "text.txt";$fp = fopen($filename,"r");$buffer = "";while(!feof($fp)){    $buffer .= fread($fp,1024);}fclose($fp);$buffer = preg_replace("/[0-9]+.\s/","",$buffer);$fp = fopen($filename,"w");fwrite($fp,$buffer);fclose($fp);

這是最簡單的過濾,更複雜的過濾只需要更換Regex即可。

PHP 檔案系統管理函數與 preg_replace() 函數過濾代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.