自动转换字符集 支持数组转换

@param $string
@param string $from
@param string $to

@return array|string
767953
public static function autoCharset($string, $from = 'gbk', $to = 'utf-8')
    {
        $from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
        $to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
        if (strtoupper($from) === strtoupper($to) || empty($string) || (is_scalar($string) && !is_string($string))) {
            //如果编码相同或者非字符串标量则不转换
            return $string;
        }
        if (is_string($string)) {
            if (function_exists('mb_convert_encoding')) {
                return mb_convert_encoding($string, $to, $from);
            } elseif (function_exists('iconv')) {
                return iconv($from, $to, $string);
            } else {
                return $string;
            }
        } elseif (is_array($string)) {
            foreach ($string as $key => $val) {
                $_key = self::autoCharset($key, $from, $to);
                $string[$_key] = self::autoCharset($val, $from, $to);
                if ($key != $_key)
                    unset($string[$key]);
            }
            
            return $string;
        } else {
            return $string;
        }
    }
1172711
public static function DateRangeDiff(string $from, string $to)
    {
        $date_Range = new \Phunc\DateRange\DateRange();
        $date_Range->fillFromString($from, $to);
        $date_Range->getFrom()->hour(0)->minute(0)->second(0);
        $date_Range->getTo()->hour(24); //->minute(59)->second(59);
        return $date_Range->diffInDays();
    }
906684
public function casing ($string, $to = '')
    {
        $res = null;
        
        // Pick one! :)
        switch ($to) {
            case 'lower':
                $res = mb_strtolower($string);
                break;
            case 'upper':
                $res = mb_strtoupper($string);
                break;
            case 'title':
                $res = ucfirst(strtolower($string));
                break;
            default:
                $res = $string;
                break;
        }
        
        return $res;
    }
104314
protected static function recode( $string, $fromCharset, $toCharset, $stop = false )
    {
        $codec = eZTextCodec::instance( $fromCharset, $toCharset, false );
        if ( $codec )
        {
            $string = $codec->convertString( $string );
        }

        return $string;
    }
889193
private function getRelativeUrl($from, $to)
	{
		$relative = '';
		while (true)
		{
			if ($from === $to)
				return $relative;
			else if ($from === dirname($from))
				return $relative . substr($to, 1);
			if (!strncmp($from . '/', $to, strlen($from) + 1))
				return $relative . substr($to, strlen($from) + 1);

			$from = dirname($from);
			$relative .= '../';
		}
	}
141710
public function rename($from, $to)
    {
        if (is_null($key = $this->search($from))) {
            return false;
        }

        $this->items[$key] = $this->getSlugFromName($to);

        return true;
    }
167468
private function copyFromTo($from, $to)
    {
        $directory = dirname($to);
        if (!is_dir($directory)) {
            mkdir($directory, 0777, true);
        }

        copy($from, $to);
    }
912893
public function from($from, $from_name = '')
    {
        $this->from      = $from;
        $this->from_name = $from_name;
        return $this;
    }
639551
public static function convertEncoding($string, $to, $from)
    {
        if (!function_exists('mb_convert_encoding'))
        {
            throw new \RuntimeException(
                'StringEscaper requires mbstring extension to be installed when escaping for non UTF-8 strings.'
            );
        }

        //Convert character encoding
        $result = mb_convert_encoding($string, $to, $from);

        // Return non-fatal blank string on encoding errors from users
        $result = $result !== false ? $result : '';

        return $result;
    }
237954
private function getUrl($string, $from, $to, $key)
    {
        return sprintf(
            'https://www.googleapis.com/language/translate/v2?key=%s&source=%s&target=%s&q=%s',
            $key,
            $from,
            $to,
            urlencode($string)
        );
    }
230326
private function resolveTo($to, $from)
    {
        if (is_string($to)) {
            $to = strtolower($to);
        }

        if ($to === 'down') {
            $to = $this->versionCollection->getPreviousVersion($from);
        }

        if ($to === 'up') {
            $to = $this->versionCollection->getNextVersion($from);
        }

        if ($to === 'bottom') {
            $to = 0;
        }

        if ($to === 'top' || null === $to) {
            $to = $this->versionCollection->getLatestVersion();
        }

        if (0 !== $to && false === strtotime($to)) {
            throw new MigratorException(sprintf(
                'Unknown migration action "%s". Known actions: "%s"',
                $to,
                implode('", "', $this->actions)
            ));
        }

        if (0 !== $to && !$this->versionCollection->has($to)) {
            throw new MigratorException(sprintf(
                'Unknown version "%s"', $to
            ));
        }

        return $to;
    }
874224
private function mergeArrayFromIndex(array $to, array $from, $index = 0)
    {
        $size = count($from);

        if ($size > $index) for ($i = $index; $i < $size; ++$i) $to[] = $from[$i];

        return $to;
    }
1340679
public static function move($names, &$from, &$to)
    {
        if (is_array($from) && is_array($to)) {
            foreach ($names as $key) {
                if (isset($from[$key]) && !isset($to[$key])) {
                    $to[$key] = static::getValue($from, $key);
                    $moved[$key] = static::removeValue($from, $key);
                }
            }
        }
        return $moved;
    }
488609
public function renameMailbox($from = '', $to = '')
    {
        if (empty($from) || empty($to)) {
            return false;
        }

        $from = imap_utf7_encode($from);
        $to = imap_utf7_encode($to);

        return imap_renamemailbox($this->conn, "{".$this->settings['server']."}INBOX.".$from,
            "{".$this->settings['server']."}INBOX.".$to);
    }
996508
public function from($from = null) {
        if ($from === null) {
            return $this->from;
        }

        $this->from = (int) $from;

        return $this;
    }
1327498
protected function longestCommonSubsequence(array $from, array $to)
    {
        $common     = array();
        $matrix     = array();
        $fromLength = count($from);
        $toLength   = count($to);

        for ($i = 0; $i <= $fromLength; ++$i) {
            $matrix[$i][0] = 0;
        }

        for ($j = 0; $j <= $toLength; ++$j) {
            $matrix[0][$j] = 0;
        }

        for ($i = 1; $i <= $fromLength; ++$i) {
            for ($j = 1; $j <= $toLength; ++$j) {
                $matrix[$i][$j] = max(
                  $matrix[$i-1][$j],
                  $matrix[$i][$j-1],
                  $from[$i-1] === $to[$j-1] ? $matrix[$i-1][$j-1] + 1 : 0
                );
            }
        }

        $i = $fromLength;
        $j = $toLength;

        while ($i > 0 && $j > 0) {
            if ($from[$i-1] === $to[$j-1]) {
                array_unshift($common, $from[$i-1]);
                --$i;
                --$j;
            }

            else if ($matrix[$i][$j-1] > $matrix[$i-1][$j]) {
                --$j;
            }

            else {
                --$i;
            }
        }

        return $common;
    }
220713
public function search(string $from, string $to) : array
    {
        if (!isset($this->nodes[$from]) || !isset($this->nodes[$to])) {
            throw new \UnexpectedValueException("node {$from} or node {$to} does not exist");
        }
        // initialization
        $nodes      = array_keys($this->nodes);
        $distance   = [];
        $parent     = [];
        $visit      = [];

        foreach ($this->nodes as $key => $value) {
            $distance[$key] = $this->total_cost +1;
        }
        $distance[$from] = 0;

        // set start node
        foreach ($this->nodes as $key => $val) {
            $parent[$key] = null;
        }
        foreach ($this->nodes[$from] as $key => $val) {
            $distance[$key] = $this->nodes[$from][$key];
            $parent[$key]   = $from;
        }
        $visit[] = $from;

        // search shortest route
        for (;;) {
            $min_distance   =   $this->total_cost + 1;
            $node           =   null;
            foreach (array_diff($nodes, $visit) as $key) {
                if ($distance[$key] < $min_distance) {
                    $node           =   $key;
                    $min_distance   =   $distance[$key];
                }
            }
            if ($node === $to) {
                break;
            } elseif (is_null($node)) {
                throw new \UnexpectedValueException("path from {$from} to {$to} does not exist");
            }
            foreach (array_diff(array_keys($this->nodes[$node]), $visit) as $key) {
                if ($distance[$key] > $distance[$node] + $this->nodes[$node][$key]) {
                    $distance[$key] = $distance[$node] + $this->nodes[$node][$key];
                    $parent[$key]   = $node;
                }
            }
            $visit[] = $node;
        }

        $result = [];
        for (;;) {
            $result[] = $node;
            if ($node === $from) {
                break;
            }
            $node = $parent[$node];
        }
        return array_reverse($result);
    }