2.6. PHP

2.6.1. PHP学习记录

2.6.2. crifan的PHP库

暂时还没有合并所有的php相关的函数到crifanLib.php中,此处只是整理出目前已有的一些有用的函数而已。

2.6.2.1. 与字符串等有关的函数

2.6.2.1.1. 添加末尾的斜杠:addTailSlash
//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 />";
                    

2.6.2.1.2. 去除开始的斜杠:removeFirstSlash
//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;
}
                
2.6.2.1.3. 检查一个字符串是否在另一个数组中:strExistInArr
//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;
}
                

例 2.2. strExistInArr使用范例

$fileValid = $this->strExistInArr($fileSuf, $validSuf);
                    

2.6.2.1.4. 从文件过滤类型生成文件后缀列表:genValidSuffix

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

2.6.2.2. 与文件等有关的函数

2.6.2.2.1. 获得文件名的后缀:getFileSuffix
//eg: from abc.txt got txt
function getFileSuffix($fileName)
{
    $gotInfo = pathinfo($fileName);
    // dirname, basename, extension
    $suffix = strtolower($gotInfo["extension"]);
    return $suffix;
}
                

例 2.4. getFileSuffix使用范例

$fileSuf = $this->getFileSuffix($filename);
                    

2.6.2.2.2. 如何获得某个文件下面的文件(和子文件夹)

相关的代码如下,供参考:

$dirHandler = opendir($fullPath);

while(($filename = readdir($dirHandler)) !== false)
{
    if(($filename == ".") || ($filename == "..") )
    {
        //echo "omit filename=$filename";
        continue;
    }

    echo "filename=". $filename";
}
closedir($dirHandler);