日期:2012-11-16  浏览次数:20449 次

<?
//------------------------
// PHP内置字符串函数实现
//------------------------

//字符串长度
function strlen ($str)
{
        if ($str == '' ) return 0;
        $count =  0;
        while (1){
                if ( $str[$count] != NULL){
                         $count++;
                        continue;
                }else{
                        break;
                }
        }
        return $count;
}

//截取子串
function substr($str, $start,  $length=NULL)
{
        if ($str== '' || $start>strlen($str )) return;
        if (($length!=NULL) && ( $start>0) && ($length> strlen($str)-$start)) return;
        if (( $length!=NULL) && ($start< 0) && ($length>strlen($str )+$start)) return;
        if ($length ==  NULL) $length = (strlen($str ) - $start);
        
        if ($start <  0){
                for ($i=(strlen( $str)+$start); $i<(strlen ($str)+$start+$length ); $i++) {
                        $substr .=  $str[$i];
                }
        }
        if ($length  > 0){
                for ($i= $start; $i<($start+$length ); $i++) {
                        $substr  .= $str[$i];
                }
        }
        if ( $length < 0){
                for ($i =$start; $i<(strlen( $str)+$length); $i++) {
                        $substr .= $str[$i ];
                }
        }
        return $substr;
}

//字符串翻转
function strrev($str)
{
        if ($str == '') return 0 ;
        for ($i=(strlen($str)- 1); $i>=0; $i --){
                $rev_str .= $str[$i ];
        }