``` /** * 字符串替换 * @param string $str 要替换的字符串 * @param string $repStr 即将被替换的字符串 * @param int $start 要替换的起始位置,从0开始 * @param string $splilt 遇到这个指定的字符串就停止替换 */ function StrReplace($str,$repStr,$start,$splilt = ''){ $newStr = substr($str,0,$start); $breakNum = -1; for ($i=$start;$i<strlen($str);$i++){ $char = substr($str,$i,1); if($char==$splilt){ $breakNum = $i; break; } $newStr.=$repStr; } if($splilt!='' && $breakNum>-1){ for ($i=$breakNum;$i<strlen($str);$i++){ $char = substr($str,$i,1); $newStr.=$char; } } return $newStr; } ``` ``` /** * 截取字符串 */ function StrSub($str, $start = 0, $length, $charset = "utf-8", $suffix = false){ $newStr = ''; if (function_exists ( "mb_substr" )) { if ($suffix){ $newStr = mb_substr ( $str, $start, $length, $charset )."..."; }else{ $newStr = mb_substr ( $str, $start, $length, $charset ); } } elseif (function_exists ( 'iconv_substr' )) { if ($suffix){ $newStr = iconv_substr ( $str, $start, $length, $charset )."..."; }else{ $newStr = iconv_substr ( $str, $start, $length, $charset ); } } if($newStr==''){ $re ['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"; $re ['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"; $re ['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"; $re ['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"; preg_match_all ( $re [$charset], $str, $match ); $slice = join ( "", array_slice ( $match [0], $start, $length ) ); if ($suffix) $newStr = $slice; } return $newStr; } ```