计算字符长度
@param  [type] $str
@return int|string [type]
973342
public static function length($str)
    {
        if (empty($str)) {
            return '0';
        }

        if ((string)$str === '0') {
            return '1';
        }

        if (\function_exists('mb_strlen')) {
            return mb_strlen($str, 'utf-8');
        }

        preg_match_all('/./u', $str, $arr);

        return \count($arr[0]);
    }
122875
public function token($str) {
        $str=trim(preg_replace('/\{\{(.+?)\}\}/s',trim('\1'),
            Base::instance()->compile($str)));
        if (preg_match('/^(.+)(?<!\|)\|((?:\h*\w+(?:\h*[,;]?))+)$/s',
            $str,$parts)) {
            $str=trim($parts[1]);
            foreach (Base::instance()->split($parts[2]) as $func)
                $str=is_string($cmd=$this->filter($func))?
                    $cmd.'('.$str.')':
                    'Base::instance()->'.
                    'call($this->filter(\''.$func.'\'),['.$str.'])';
        } elseif (false !== strpos($str, '(')) {
            for ($i=0,$len=strlen($str),$newStr='',$tmp='';$i<$len;) {
                if ($str[$i] === '(') {
                    if ($tmp && array_key_exists($tmp, $this->filter)) {
                        $tmp = $this->filter[$tmp];
                    }
                    $newStr .= $tmp . '(';
                    $tmp = '';
                } elseif (in_array($str[$i], [' '])) {
                    $newStr .= $tmp . $str[$i];
                    $tmp = '';
                } else {
                    $tmp .= $str[$i];
                }
                $i++;
            }
            $newStr .= $tmp;
            $str = $newStr;
        }

        return $str;
    }
1348183
public function decode($str) {

		$str = strtoupper($str);

		$l = strlen($str);
		$n = 0;
		$b = 0;

		$decoded = null;

		for ($i = 0; $i < $l; $i++) {
			$n = $n << 5;
			$n = $n + $this->decodeTable[$str[$i]];
			$b = $b + 5;

			if ($b >= 8) {
				$b = $b - 8;
				$decoded .= chr(($n & (0xFF << $b)) >> $b);
			}
		}

		return $decoded;
	}
369868
public function parse($str)
    {
        $this->removeBom($str);

        $data = $this->createM3uData();
        $lines = \explode("\n", $str);

        for ($i = 0, $l = \count($lines); $i < $l; ++$i) {
            $lineStr = \trim($lines[$i]);
            if ('' === $lineStr || $this->isComment($lineStr)) {
                continue;
            }

            if ($this->isExtM3u($lineStr)) {
                $tmp = \trim(\substr($lineStr, 7));
                if ($tmp) {
                    $data->initAttributes($tmp);
                }
                continue;
            }

            $data->append($this->parseLine($i, $lines));
        }

        return $data;
    }
847820
private function checkSignature($str='')
    {
        $signature = isset($_GET["signature"])?$_GET["signature"]:'';
        $signature = isset($_GET["msg_signature"])?$_GET["msg_signature"]:$signature; //如果存在加密验证则用加密验证段
        $timestamp = isset($_GET["timestamp"])?$_GET["timestamp"]:'';
        $nonce = isset($_GET["nonce"])?$_GET["nonce"]:'';

        $token = $this->token;
        $tmpArr = array($token, $timestamp, $nonce,$str);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
368477
public static function stringToArray($str)
    {
        $arr = [];

        for ($i = 0; $i < self::stringLength($str); $i++) {
            $arr[] = function_exists('mb_substr') ? mb_substr($str, $i, 1) : $str[$i];
        }

        return $arr;
    }
1019111
protected static function str2bits($str) 
    {
        $bits  = '';
        $total = strlen($str);

        $i = -1;
        while (++$i < $total) {
            $char = $str[$i];
            $pos  = strpos(self::$_encode, $char);

            if ($pos !== false) {
                $bits .= substr(self::$_binary, $pos*5, 4);
            } else {
                $int   = ord($char);
                $bits .= '1'.substr(self::$_bin, (int)($int/16)*5, 4);
                $bits .= substr(self::$_bin, ($int%16)*5, 4);
            }
        }

        // Complete nibbel
        $add   = strlen($bits) % 4;
        $bits .= str_repeat('0', $add);

        return $bits;
    }
448980
static private function _checkString($str,$v)
    {
        foreach($v as $key=>$value)
        {
            if (preg_match("/".$value."/is", $str) == 1 || preg_match("/".$value."/is", urlencode($str)) == 1)
            {
                //W_log("<br>IP: ".$_SERVER["REMOTE_ADDR"]."<br>时间: ".strftime("%Y-%m-%d %H:%M:%S")."<br>页面:".$_SERVER["PHP_SELF"]."<br>提交方式: ".$_SERVER["REQUEST_METHOD"]."<br>提交数据: ".$str);
                return $str;
            }
        }
        
        return null;
    }
1157186
private function parse_all(&$str)
    {
        // php include
//         $str = preg_replace ( "/\{include\s+(.+)\}/", "<?php include \\1;?\>", $str );
        
        // php code, 内容分析到 '}', 可能会有问题？
        $str = preg_replace ( "/\{php\s+([^\}]+)\}/", "<?php \\1?>", $str );
        
        // if else
        $str = preg_replace ( "/\{if\s+(.+?)\}/", "<?php if(\\1) { ?>", $str );
        $str = preg_replace ( "/\{else\}/", "<?php } else { ?>", $str );
        $str = preg_replace ( "/\{elseif\s+(.+?)\}/", "<?php } elseif (\\1) { ?>", $str );
        $str = preg_replace ( "/\{\/if\}/", "<?php } ?>", $str );
        
        // for : {for($i=1; $i<10; $i++)}
        $str = preg_replace("/\{for\s+\(?(.+?)\)?\}/","<?php for(\\1) { ?>", $str);
        $str = preg_replace("/\{\/for\}/", "<?php } ?>", $str);
        
        // foreach : 
        // 1. {foreach($array as [$key=>] $value)}
        // 2. {foreach $array $value}
        // 3. {foreach $array $key $value}
        $str = preg_replace("/\{(foreach|loop)\s+\(((\S+).+?)\)\}/","<?php if(isset(\\3) && is_array(\\3)) foreach(\\2) { ?>", $str);
        $str = preg_replace("/\{(foreach|loop)\s+(\S+)\s+(?:as\s+)*(\S+)\}/", "<?php if(isset(\\2) && is_array(\\2)) foreach(\\2 as \\3) {?>", $str );
        $str = preg_replace("/\{(foreach|loop)\s+(\S+)\s+(?:as\s+)*(\S+)\s+(\S+)\}/", "<?php if(isset(\\2) && is_array(\\2)) foreach(\\2 as \\3 => \\4){?>", $str );
        $str = preg_replace("/\{\/(foreach|loop)\}/", "<?php } ?>", $str );
        
        // ++ --
        $str = preg_replace("/\{\+\+(.+?)\}/", "<?php ++\\1; ?>", $str);
        $str = preg_replace("/\{\-\-(.+?)\}/", "<?php ++\\1; ?>", $str);
        $str = preg_replace("/\{(.+?)\+\+\}/", "<?php \\1++; ?>", $str);
        $str = preg_replace("/\{(.+?)\-\-\}/", "<?php \\1--; ?>", $str);
        
        // {widget:name action='method'...}  调用标签方法
        $str = preg_replace_callback("/\{widget:?([\w\:]+)?\s+([^}]+)\}/i", array($this,'widget_callback'), $str);
        $str = preg_replace_callback("/\{\/widget[^}]*\}/i", array($this, 'end_widget_callback'), $str);
        
        // include sub template: {include 'header'}
        $str = preg_replace_callback( "/\{(template|include|layout)\s+(.+)\}/", array($this, 'parse_callback'), $str );
        
        // function or var: {function(...)} or {$data}
        $str = $this->parse_fun_var($str);
        
        // add permission
        
        return $str;
    }
1365096
public function decrypt(string $str): string
    {
        return openssl_decrypt($str, self::CIPHER_METHOD, $this->key, 0, $this->getIv());
    }
820905
public function str2int ($str) 
    {
        // Normalization phase
        $str = str_replace(array('أ','إ','آ'), 'ا', $str);
        $str = str_replace('ه', 'ة', $str);
        $str = preg_replace('/\s+/', ' ', $str);
        $ptr = array('ـ', 'َ','ً','ُ','ٌ','ِ','ٍ','ْ','ّ');
        $str = str_replace($ptr, '', $str);
        $str = str_replace('مائة', 'مئة', $str);
        $str = str_replace(array('احدى','احد'), 'واحد', $str);
        $ptr = array('اثنا','اثني','اثنتا', 'اثنتي');
        $str = str_replace($ptr, 'اثنان', $str);
        $str = trim($str);
        
        if (strpos($str, 'ناقص') === false
            && strpos($str, 'سالب') === false
        ) {
            $negative = false;
        } else {
            $negative = true;
        }
        
        // Complications process
        $segment = array();
        $max     = count($this->_complications);
        
        for ($scale=$max; $scale>0; $scale--) {
            $key = pow(1000, $scale);
            
            $pattern = array('أ','إ','آ');
            $format1 = str_replace($pattern, 'ا', $this->_complications[$scale][1]);
            $format2 = str_replace($pattern, 'ا', $this->_complications[$scale][2]);
            $format3 = str_replace($pattern, 'ا', $this->_complications[$scale][3]);
            $format4 = str_replace($pattern, 'ا', $this->_complications[$scale][4]);
            
            if (strpos($str, $format1) !== false) {
                list($temp, $str) = explode($format1, $str);
                $segment[$key]    = 'اثنان';
            } elseif (strpos($str, $format2) !== false) {
                list($temp, $str) = explode($format2, $str);
                $segment[$key]    = 'اثنان';
            } elseif (strpos($str, $format3) !== false) {
                list($segment[$key], $str) = explode($format3, $str);
            } elseif (strpos($str, $format4) !== false) {
                list($segment[$key], $str) = explode($format4, $str);
                if ($segment[$key] == '') {
                    $segment[$key] = 'واحد';
                }
            }
            
            if (isset($segment[$key]) && $segment[$key] != '') {
                $segment[$key] = trim($segment[$key]);
            }
        }
        
        $segment[1] = trim($str);

        // Individual process
        $total    = 0;
        $subTotal = 0;
        
        foreach ($segment as $scale => $str) {
            $str = " $str ";
            foreach ($this->_spell as $word => $value) {
                if (strpos($str, "$word ") !== false) {
                    $str = str_replace("$word ", ' ', $str);
                    $subTotal += $value;
                }
            }

            $total   += $subTotal * $scale;
            $subTotal = 0;
        }
        
        if ($negative) {
            $total = -1 * $total;
        }
        
        return $total;
    }
1217866
public static function utf7_to_utf8($str)
    {
        $Index_64 = array(
            0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
            0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
            0,0,0,0, 0,0,0,0, 0,0,0,1, 0,0,0,0,
            1,1,1,1, 1,1,1,1, 1,1,0,0, 0,0,0,0,
            0,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1,
            1,1,1,1, 1,1,1,1, 1,1,1,0, 0,0,0,0,
            0,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1,
            1,1,1,1, 1,1,1,1, 1,1,1,0, 0,0,0,0,
        );

        $u7len = strlen($str);
        $str   = strval($str);
        $res   = '';

        for ($i=0; $u7len > 0; $i++, $u7len--) {
            $u7 = $str[$i];
            if ($u7 == '+') {
                $i++;
                $u7len--;
                $ch = '';

                for (; $u7len > 0; $i++, $u7len--) {
                    $u7 = $str[$i];

                    if (!$Index_64[ord($u7)]) {
                        break;
                    }

                    $ch .= $u7;
                }

                if ($ch == '') {
                    if ($u7 == '-') {
                        $res .= '+';
                    }

                    continue;
                }

                $res .= self::utf16_to_utf8(base64_decode($ch));
            }
            else {
                $res .= $u7;
            }
        }

        return $res;
    }
772718
public static function roman2arabic(string $str): int
	{
		if(empty($str) || preg_match('/^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/i', $str) === 0)
		{
			throw new InvalidArgumentException('Invalid roman numeral. Only values between I and MMMCMXCIX are allowed.');
		}

		$numerals =
		[
			'M'  => 1000,
			'D'  => 500,
			'C'  => 100,
			'L'  => 50,
			'X'  => 10,
			'V'  => 5,
			'I'  => 1,
		];

		$arabicNumeral = 0;

		$previous = 0;

		foreach(str_split($str) as $numeral)
		{
			$arabicNumeral += $numerals[$numeral];

			if($previous < $numerals[$numeral])
			{
				$arabicNumeral -= ($previous * 2);
			}

			$previous = $numerals[$numeral];
		}

		return $arabicNumeral;
	}
339344
protected function _p($str) {
		$str=trim($str);
		if (strlen($str)) {
			if (preg_match('/^(.+?\n)([>#].+)$/s',$str,$parts))
				return $this->_p($parts[1]).$this->build($parts[2]);
			$str=preg_replace_callback(
				'/([^<>\[]+)?(<[\?%].+?[\?%]>|<.+?>|\[.+?\]\s*\(.+?\))|'.
				'(.+)/s',
				function($expr) {
					$tmp='';
					if (isset($expr[4]))
						$tmp.=$this->esc($expr[4]);
					else {
						if (isset($expr[1]))
							$tmp.=$this->esc($expr[1]);
						$tmp.=$expr[2];
						if (isset($expr[3]))
							$tmp.=$this->esc($expr[3]);
					}
					return $tmp;
				},
				$str
			);
			$str=preg_replace('/\s{2}\r?\n/','<br />',$str);
			return '<p>'.$this->scan($str).'</p>'."\n\n";
		}
		return '';
	}
1005255
private function MaxAge($str) {
    if (strpos($str, '.') === false) {
      throw new Exception('File has no extension');
    }
    $str = strrchr($str, '.');
    $str = substr($str, 1);
    return (integer) $str;
  }
267510
public static function load($str)
    {
        $dom       = new Dom;
        self::$dom = $dom;

        return $dom->load($str);
    }
82468
public function widont($str) {
		$str = rtrim($str);
		$space = strrpos($str, ' ');

		if ($space !== false) {
			$str = substr($str, 0, $space) . '&nbsp;' . substr($str, $space + 1);
		}

		return $str;
	}