转换字符集编码
@param $data
@param $targetCharset
@return string
355844
function characet($data, $targetCharset) {
		
		if (!empty($data)) {
			$fileType = $this->fileCharset;
			if (strcasecmp($fileType, $targetCharset) != 0) {
				$data = mb_convert_encoding($data, $targetCharset, $fileType);
				//				$data = iconv($fileType, $targetCharset.'//IGNORE', $data);
			}
		}


		return $data;
	}
1319567
public function view($data)
    {
        //align
        switch ($this->align) {
            case 'center': $a = ['text-align'=>'center']; break;
            case 'right': $a = ['text-align'=>'right']; break;
            case 'left':
            default: $a = ['text-align'=>'left'];
        }
        $this->styles['list_div'] = array_merge($this->styles['list_div'], $a);
        //width
        $wid = floor(100 / $this->columns) - 1;
        //data and formatter
        $this->styles['list_item'] = array_merge($this->styles['list_item'], ['width'=>"$wid%"]);
        if (array_key_exists('data', $data)) {
            $d = $data['data'];
        } else {
            $d = $data;
        }
        if (array_key_exists('label_formatter', $data)) {
            $lf = $data['label_formatter'];
        }
        if (array_key_exists('text_formatter', $data)) {
            $tf = $data['text_formatter'];
        }
        foreach ($d as $k => $v) {
            $ltext = $k;
            if(isset($lf) && is_callable($lf)) {
                $ltext = $lf($k);
            }
            $label = new Element('label', new Text($ltext));
            $label->addClass($this->getClass('list_label'))->setAttribute('style', $this->getStyle('list_label'));
            $ttext = $v;
            if(isset($tf) && is_callable($tf)) {
                $ttext = $tf($v);
            }
            $text = new Element('span', new Text($ttext));
            $text->addClass($this->getClass('list_text'))->setAttribute('style', $this->getStyle('list_text'));
            $item = new Element('div');
            $item->addClass('list_item')->setAttribute('style', $this->getStyle('list_item'));
            $item->appendChild($label)->appendChild($text);
            $this->appendChild($item);
            $this->addClass('list_div')->setAttribute('style', $this->getStyle('list_div'));
        }
        return $this->render();
    }
1023606
protected function cleanData($data)
    {
        $replacementChars = array(
            "\xe2\x80\x98" => "'",   // ‘
            "\xe2\x80\x99" => "'",   // ’
            "\xe2\x80\x9a" => "'",   // ‚
            "\xe2\x80\x9b" => "'",   // ‛
            "\xe2\x80\x9c" => '"',   // “
            "\xe2\x80\x9d" => '"',   // ”
            "\xe2\x80\x9e" => '"',   // „
            "\xe2\x80\x9f" => '"',   // ‟
            "\xe2\x80\x93" => '-',   // –
            "\xe2\x80\x94" => '--',  // —
            "\xe2\x80\xa6" => '...', // …
            "\xc2\xa0"     => ' ',
        );
        // Replace UTF-8 characters
        $cleanedData = strtr($data, $replacementChars);

        // Replace Windows-1252 equivalents
        $charsToReplace = array_map(function ($code) {
            return $this->mb_chr($code);
        }, array(133, 145, 146, 147, 148, 150, 151, 194));
        $cleanedData = $this->mb_str_replace($charsToReplace, $replacementChars, $cleanedData);

        return $cleanedData;
    }
749520
public function update($data = null): IResult
    {
        $query = static::query($this);
        $query = $this->parseWhereId($query);
        if(null === $data)
        {
            $data = static::parseSaveData($this);
        }
        else if(!$data instanceof \ArrayAccess)
        {
            $data = new LazyArrayObject($data);
        }

        // 更新前
        $this->trigger(ModelEvents::BEFORE_UPDATE, [
            'model' => $this,
            'data'  => $data,
            'query' => $query,
        ], $this, \Imi\Model\Event\Param\BeforeUpdateEventParam::class);

        if(!isset($query->getOption()->where[0]))
        {
            throw new \RuntimeException('use Model->update(), primary key can not be null');
        }

        $result = $query->update($data);

        // 更新后
        $this->trigger(ModelEvents::AFTER_UPDATE, [
            'model' => $this,
            'data'  => $data,
            'result'=> $result,
        ], $this, \Imi\Model\Event\Param\AfterUpdateEventParam::class);

        // 子模型更新
        ModelRelationManager::updateModel($this);

        return $result;
    }
1298770
public function readString(string $data)
    {
        $reader = new XMLReader;
        $reader->XML($data);

        $contents = $this->toArray($reader);
        $reader->close();

        return $contents;
    }
85774
private static function getLabel(string $data): string
    {
        $result = explode(':', rawurldecode(substr($data, 1)));

        return 2 === count($result) ? $result[1] : $result[0];
    }
433012
protected static function getUrlSafeData($data)
    {
        $remainder = strlen($data) % 4;

        if ($remainder) {
            $data .= str_repeat('=', 4 - $remainder);
        }

        return strtr($data, '-_', '+/');
    }
1352219
public function encodeArray($data) {
        $d = array();
        foreach ($data as $key => $value) {
            if (is_string($key))
                $key = htmlspecialchars($key);
            if (is_string($value))
                $value = htmlspecialchars($value);
            elseif (is_array($value))
                $value = self::encodeArray($value);
            $d[$key] = $value;
        }
        return $d;
    }
830265
private function htmlEncode($data)
    {
        if (empty($data)) {
            return null;
        }

        $safeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890._-";

        $result = "";
        for ($i = 0; $i < strlen($data); $i++) {
            if (strstr($safeChars, $data[$i])) {
                $result .= $data[$i];
            } else if ("7F" >= $var = bin2hex(substr($data, $i, 1))) {
                $result .= "&#x" . $var . ";";
            } else {
                $result .= $data[$i];
            }
        }

        return $result;
    }
1010349
static function parseHeader($data)
    {
        $header = array();
        $header[0] = array();
        $meta = &$header[0];
        $parts = explode("\r\n\r\n", $data, 2);

        // parts[0] = HTTP头;
        // parts[1] = HTTP主体，GET请求没有body
        $headerLines = explode("\r\n", $parts[0]);

        // HTTP协议头,方法，路径，协议[RFC-2616 5.1]
        list($meta['method'], $meta['uri'], $meta['protocol']) = explode(' ', $headerLines[0], 3);

        //错误的HTTP请求
        if (empty($meta['method']) or empty($meta['uri']) or empty($meta['protocol']))
        {
            return false;
        }
        unset($headerLines[0]);
        //解析Header
        $header = array_merge($header, self::parseHeaderLine($headerLines));
        return $header;
    }
1012500
protected function convertRowCharset(array $row, $inputCharset, $fileCharset)
    {
        foreach ($row as $k => $v) {
            $row[$k] = $this->getConverter()->convert($v, $inputCharset, $fileCharset);
        }

        return $row;
    }
731189
public static function get_matching_tourdata(\moodle_url $targetmatch) {
        $tours = self::get_enabled_tourdata();

        // Attempt to determine whether this is the front page.
        // This is a special case because the frontpage uses a shortened page path making it difficult to detect exactly.
        $isfrontpage = $targetmatch->compare(new \moodle_url('/'), URL_MATCH_BASE);
        $target = $targetmatch->out_as_local_url();
        return array_filter($tours, function($tour) use ($isfrontpage, $target) {
            if ($isfrontpage && $tour->pathmatch === 'FRONTPAGE') {
                return true;
            }
            $pattern = preg_quote($tour->pathmatch, '@');
            $pattern = str_replace('%', '.*', $pattern);
            return !!preg_match("@{$pattern}@", $target);
        });
    }
402422
public function make_sign($data){
        /*去空*/
        $data=array_filter($data);
        /*签名步骤一：按字典序排序参数*/
        ksort($data);
        $string_a=http_build_query($data);
        $string_a=urldecode($string_a);

        /*签名步骤二：在string后加入KEY*/
        $config=$this->config;

        $string_sign_temp=$string_a."&key=".$config['key'];
        /*签名步骤三：MD5加密*/
        $sign = md5($string_sign_temp);
        /*签名步骤四：所有字符转为大写*/
        $result=strtoupper($sign);
        return $result;
    }
874439
public static function encode($data)
    {
        if (!is_string($data)) {
            throw new InvalidArgumentException('data must be string');
        }

        $encodedData = parent::encode($data);

        // URL safe replacement and remove padding
        return rtrim(strtr($encodedData, '+/', '-_'), '=');
    }
1321133
private static function _handleTypeOutput($data, $hasType = true, $mark = true, $outString = '')
    {
        # 常规打印，不含有类型
        if (!$hasType) {
            return self::_handleNormalOutput($data);
        }

        $jsClass = self::$controlClass;
        $html = 'htmlspecialchars';
        $ucfirst = 'ucfirst';
        static $i = 1;
        $usualString = "<dt><div class=\"array-value\">\n%s</div></dt>\n";
        $outString .= '<dl>' . PHP_EOL;
        $dataType = gettype($data);

        // if (is_object($data)) $data = (array)$data;
        # 含类型打印
        if (is_array($data)) {
            $count = 'count';
            $mark && $outString .= "<dt>\n<span class=\"print-icon icon-hide\"></span><div class=\"{$jsClass}\" style=\"width:98%\">" .
                "<strong class=\"general-print-color-dg\">{$ucfirst($dataType)}</strong>(size:<strong>{$count($data)}</strong>)<strong>(</strong></div>\n</dt>\n";
            $outString .= '<!-- .general-print-ar-content -->' . PHP_EOL . '<dd class="general-print-ar-content">';
            $tab = self::_getTab($i);

            foreach ($data as $k => $v) {
                $vType = gettype($v);
                $k = is_int($k) ? $k : "'{$html($k,ENT_QUOTES)}'";
                $outString .= "\n<dl>\n<dt><div class=\"array-key\">$tab$k</div>";

                if (is_array($v) || is_object($v) || is_resource($data)) {//
                    $outString .= "<div class=\"array-value {$jsClass}\"> &rArr; <strong class=\"general-print-color-dg\">{$ucfirst($vType)}</strong>(size:";

                    if (empty($v)) {
                        $outString .= '0)<strong>()</strong> </div><span class="print-icon icon-hide"></span></dt>';
                        continue;
                    }
                    $i++;

                    $outString .= "<strong>{$count((array)$v)}</strong>)<strong>(</strong></div>\n<span class=\"print-icon icon-hide\"></span></dt>\n";
                    $outString .= ltrim(self::_handleTypeOutput($v, true, false), '<dl>');
                } else {
                    $length_html = '';

                    if ($v === null) {
                        $v = '(null)';
                    } else if ($v === false) {
                        $v = '(false)';
                    } else if ($v === true) {
                        $v = '(true)';
                    } else {
                        $length = PrintHelper::strLength($v);
                        $length_html = "(<span class=\"general-print-color-g\">length:</span>{$length})";
                        $vType === 'string' && $v = "\"{$html($v,ENT_QUOTES)}\"";
                    }

                    $outString .= "<div class=\"array-value\"> &rArr; {$vType} <span class=\"general-print-color-r\">{$v}</span> {$length_html} </div></dt>" . PHP_EOL . "</dl>";
                }
            }//--endforeach--

            $endTab = substr($tab, 0, -($i - 1));
            $outString .= "\n</dd><!-- /.general-print-ar-content -->\n</dl>\n<dl><dt><strong>$endTab)</strong></dt>";
        } else if (is_object($data)) {
            // $outString .= PrintHelper::getSystemPrintData($data);
            $outString .= '<!-- .general-print-ar-content -->' . PHP_EOL . '<dd class="general-print-ar-content">';
            $outString .= PrintHelper::getSystemPrintData($data);
            $outString .= "\n</dd><!-- /.general-print-ar-content -->\n</dl>\n<dl><dt><strong>  )</strong></dt>";
        } else if (is_resource($data)) {
            if (($dataType = get_resource_type($data)) === 'stream' and $meta = stream_get_meta_data($data)) {

                if (isset($meta['uri'])) {
                    $file = $meta['uri'];
                    $resourceString = "Resource ({$dataType}) {$html( $file, ENT_QUOTES)}";
                } else {
                    $resourceString = "Resource ({$dataType})";
                }

            } else {
                $resourceString = "Resource ({$dataType})";
            }

            $outString .= sprintf($usualString, $resourceString);
        } else {
            $length = null;

            if ($data === false) $data = 'false';
            else if ($data === true) $data = 'true';
            else if ($data === null) $data = 'null';
            else $length = PrintHelper::strLength($data);// float integer string

            $dataType === 'string' && $data = "\"{$html($data,ENT_QUOTES)}\"";

            if ($length === null)
                $lengthString = '';
            else
                $lengthString = "(<span class=\"general-print-color-g\">length:</span>{$length})";

            $outString .= sprintf($usualString, "{$dataType} <span class=\"general-print-color-r\">{$data}</span> {$lengthString}");
        }

        return $outString . '</dl>' . PHP_EOL;
    }
1322752
public function select($data = null)
    {
        if ($data instanceof Query) {
            return $data->select();
        } elseif ($data instanceof \Closure) {
            $data($this);
            $data = null;
        }

        $this->parseOptions();

        if (false === $data) {
            // 用于子查询 不查询只返回SQL
            $this->options['fetch_sql'] = true;
        } elseif (!is_null($data)) {
            // 主键条件分析
            $this->parsePkWhere($data);
        }

        $this->options['data'] = $data;

        $resultSet = $this->connection->select($this);

        if (!empty($this->options['fetch_sql'])) {
            return $resultSet;
        }

        // 数据列表读取后的处理
        if (!empty($this->model)) {
            // 生成模型对象
            $resultSet = $this->resultSetToModelCollection($resultSet);
        } else {
            $this->resultSet($resultSet);
        }

        // 返回结果处理
        if (!empty($this->options['fail']) && count($resultSet) == 0) {
            $this->throwNotFound($this->options);
        }

        return $resultSet;
    }
912706
public function format(&$data) {
		$data = $this->normalize($data);
		$data = $this->convertToString($data);

		return $data;
	}