<?php
	function get_table_array( $charset = 'GBK',$tbprefix = 'pw_'){
		$sqldata = file_get_contents(SYS_ROOT . '/install/sql/structure.sql');
		$sqldata = str_replace(' `pw_', ' `' . $tbprefix, $sqldata);
		$sqlarr = split(';', $sqldata);
		// 过滤下数组
		$sqlarr = array_filter($sqlarr);
		$result = array();
		foreach ($sqlarr as $query_item){
			$sqlarr = preg_match_sql($query_item, $charset);
			if(!empty($sqlarr) && is_array($sqlarr)){
				$result[$sqlarr['tablename']] = $sqlarr['sql'];
			}
		}
		return $result;
	}
	function preg_match_sql($query_item,$php188_db_char){
		/* 获取建表主体串以及表属性声明串，不区分大小写，匹配换行符，且为贪婪匹配 */
		$pattern = '/^\s*(CREATE\s+TABLE[^(]+\(.*\))(.*)$/is';
		$matches = array();
        if (!preg_match($pattern, $query_item, $matches)){
            return $query_item;
        }
        $main = $matches[1];
        $postfix = $matches[2];

        /* 从表属性声明串中查找表的类型 */
        $pattern = '/.*(?:ENGINE|TYPE)\s*=\s*([a-z]+).*$/is';
        $type = preg_match($pattern, $postfix, $matches) ? $matches[1] : 'MYISAM';

        /* 从表属性声明串中查找自增语句 */
        $pattern = '/.*(AUTO_INCREMENT\s*=\s*\d+).*$/is';
        $auto_incr = preg_match($pattern, $postfix, $matches) ? $matches[1] : '';

        /* 重新设置表属性声明串 */
        $postfix = " ENGINE=$type DEFAULT CHARACTER SET " . $php188_db_char;
        $postfix .= ' ' . $auto_incr;
		$table_name = preg_replace("~CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?(`?)(\w+)\\1.*~is", "\\2", $query_item);
        /* 重新构造建表语句 */
        $sql = $main . $postfix;
		return array('sql'=>$sql, 'tablename'=>trim($table_name));
	}
	$sql_arr = get_table_array($dbcharset,$tbprefix);
?>