標籤:php 進階語言 字串函數
最近要將asp尾碼的修改成php,因懶於一個個修改。又覺得php跟Qt一樣都是進階語言了,一般進階語言都有提供對獲得的內容進行增刪改查的函數。於是乎,就上網搜了下,還真不少,故些將所用心得總結下來。
目標:將目前的目錄下的asp尾碼改成php,而不影響其它“尾碼格式的檔案”,而且只是針對“當前檔案夾”,對當前檔案夾內包含的檔案夾的檔案不進行修改。
代碼如下:
<?php function foreachDir($dirname){ if(!is_dir($dirname)) { echo "{$dirname} not effective dir"; exit(); } $handle=opendir($dirname); //開啟目錄while (($file = readdir($handle))!==false) //讀取目錄{ if($file!="." && $file!='..'){ if(is_dir($dirname.$file)){ echo $dirname.$file."<br/>"; //foreachDir($dirname.$file); //如果注釋號去掉,將會遞迴修改檔案夾內的檔案夾檔案} else{ echo "--".$dirname."/".$file."<br/>"; $temp = substr($file, strrpos($file, '.')+1); //擷取尾碼格式 if ($temp == "asp") {$pos = strripos($file,'.'); //擷取到檔案名稱的位置$filename = substr($file,0,$pos); //擷取檔案名稱rename($dirname.'/'.$file,$dirname.'/'.$filename.'.php'); //替換為php尾碼格式。 }} } } } foreachDir('../traverseMendFilename');?>
另外:附上擷取副檔名的四種方法:http://www.jb51.net/article/29765.htm
使用php修改指定檔案尾碼