暂时还没有合并所有的php相关的函数到crifanLib.php中,此处只是整理出目前已有的一些有用的函数而已。
//add the tail slash if not exist //eg: from files/doc/docbook to files/doc/docbook/ function addTailSlash($str) { if(substr($str, -1) != "/") { $str = $str."/"; } return $str; }
例 2.1. addTailSlash使用范例
//add tail slash $homePath = $this->addTailSlash($homePath); //echo "homePath=".$homePath."<br />";
//remove the first slash if exist //eg: from /files/doc/docbook to files/doc/docbook function removeFirstSlash($str) { if(substr($str, 0, 1) == "/") { $str = substr($str, 1); } return $str; }
//eg: check txt is in array("txt", "html", "pdf") function strExistInArr($str, $arr) { if($arr[0] == "*") { $found = True; } else { $found = False; $arrLen = count($arr); for($i=0; $i<$arrLen; $i++) { //if(0 == strcmp($str, $arr[$i])) if($str == $arr[$i]) { $found = True; break; } } } return $found; }
//eg: from "*.html;*.txt;" to array("html", "txt") function genValidSuffix($fileType) { $validSuf = array(); $splitedArr = explode(";", $fileType); $arrSize = count($splitedArr); //echo "splited arrary size=".$arrSize."<br />"; for($i=0; $i<$arrSize; $i++) { $singleSufDef = $splitedArr[$i]; //echo "singleSufDef=".$singleSufDef."<br />"; if($singleSufDef && ($singleSufDef != "")) { //echo "singleSufDef not null <br />"; $suffix = str_replace("*.", "", $singleSufDef); //echo "suffix=".$suffix."<br />"; if($suffix == "*") { //echo "found & suffix <br />"; $validSuf = array("*"); break; } else { //echo "add $suffix into array <br />"; array_push($validSuf, $suffix); } } } return $validSuf; }
例 2.3. genValidSuffix使用范例
//$validSuf = array("html", "htm", "txt", "pdf"); $validSuf = $this->genValidSuffix($fileType);
//eg: from abc.txt got txt function getFileSuffix($fileName) { $gotInfo = pathinfo($fileName); // dirname, basename, extension $suffix = strtolower($gotInfo["extension"]); return $suffix; }