<?php
/**
 * Tiny - A PHP Framework For Web Artisans
 * @author Tiny <tinylofty@gmail.com>
 * @copyright Copyright(c) 2010-2014 http://www.tinyrise.com All rights reserved
 * @version 1.0
 */
/**
 * String 类
 *
 * @author Tiny
 * @class TString
 */
class TString
{

	public static function msubstr($str, $start=0, $length, $charset="utf-8",$ext='...') {
        if(function_exists("mb_substr"))
            $slice = mb_substr($str, $start, $length, $charset);
        elseif(function_exists('iconv_substr')) {
            $slice = iconv_substr($str,$start,$length,$charset);
        }else{
			$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
            $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
            $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));
        }
        return strlen($str)>strlen($slice) ? $slice.$ext : $slice;
    }

	public static function parseName($name, $type=0)
	{
		if ($type) {
			return ucfirst(preg_replace("/_([a-zA-Z])/e", "strtoupper('\\1')", $name));
		} else {
			return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
		}
	}

    public static function strlen($str)
    {
        if(empty($str)) return 0;
        if(function_exists('mb_strlen')){
            return mb_strlen($str,'utf-8');
        }else{
            preg_match_all("/./u", $str, $ar);
            return count($ar[0]);
        }
    }

    public static function nl2br($str){
        return str_replace(array("\r\n", "\r", "\n"), "<br/>", $str);
    }
}
