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