SQL轉化為markdown語法
```
public function sql2md() {
/* 從文件中取出SQL語句字符串 */
$string = file_get_contents(SITE_PATH.'weibo.sql');
/* STEP1 將CREATE TABLE正則匹配出來放入數組,匹配完成后每張表的數據都儲存到數組的一個元素 */
$result = preg_match_all('/CREATE\s+TABLE.+?`(?P<name>\w+)`.+?\((?P<fields_str>.+?)PRIMARY\s+KEY\s+\(`(?P<primary_key>\w+)`\)(?P<keys>.+?)\)\sENGINE=(?P<engine>\w+).+?CHARSET=(?P<charset>[\w\d]+).+?COMMENT=\'(?P<comment>.+?)\'.+?AUTO_INCREMENT=\d+\s+;/is', $string, $matches, PREG_SET_ORDER);
$tables = array();
/* STEP2 遍歷表數據數組,每一次循環處理一張表 */
foreach ($matches as $item) {
/* STEP2-1 初始化表數據 */
$table = array(
'name' => $item['name'],
'primary_key' => $item['primary_key'],
'engine' => $item['engine'],
'charset' => $item['charset'],
'comment' => $item['comment'],
'fields' => array(),
);
/* STEP2-2 處理字段部分,即CREATE TABLE括號中的部分,每一次循環處理一個字段 */
$fields_array = explode(',', $item['fields_str']);
array_pop($fields_array);
foreach ($fields_array as $fields_array_item) {
/* STEP2-2-1 匹配字段的屬性 */
preg_match_all('/`(?P<name>\w+)`\s+(?P<type>\w+(?=\(|\s))(?P<length>\(\d+\))*.+?COMMENT\s\'(?P<comment>.+?)\'.*?/is', $fields_array_item, $matches, PREG_SET_ORDER);
$field = array(
'name' => $matches[0]['name'],
'type' => $matches[0]['type'].$matches[0]['length'],
'comment' => $matches[0]['comment'],
);
/* STEP2-2-2 判斷生成當前字段的屬性 */
$field['prop'] = array();
$field['prop']['auto_increment'] = TRUE && stripos($fields_array_item, 'auto_increment');
$field['prop']['unsigned'] = TRUE && stripos($fields_array_item, 'unsigned');
$field['prop']['zerofill'] = TRUE && stripos($fields_array_item, 'zerofill');
$field['not_null'] = TRUE && stripos($fields_array_item, 'not null');
$field['primary'] = $table['primary_key'] == $field['name'];
/* STEP2-2-3 當前字段是否有默認值,有則對其進行正則匹配 */
$has_default = TRUE && stripos($fields_array_item, 'default');
if ($has_default) {
preg_match_all('/DEFAULT\s+\'(?P<default>.*?)\'/is', $fields_array_item, $match, PREG_SET_ORDER);
$field['default'] = $match[0]['default'];
}
/* STEP2-2-4 存入返回的變量中 */
$table['fields'][] = $field;
}
/* STEP2-3 處理表的索引 */
preg_match_all('/(?P<keyname>KEY|INDEX|UNIQUE)\s+`(?P<indexname>\w+)`\s+\((?P<indexvalues>.+?)\)/is', $item['keys'], $match_keys, PREG_SET_ORDER);
if (!empty($match_keys)) {
foreach ($match_keys as $match_key) {
$match_key['indexvalues'] = str_replace('`', '', $match_key['indexvalues']);
//處理聯合索引
if (strpos($match_key['indexvalues'], ',')){
$match_key['indexvalues'] = explode(',', $match_key['indexvalues']);
}
$table['index'][] = array(
'indexname' => $match_key['indexname'],
'unique' => strtolower($match_key['keyname']) == 'unique',
'indexvalues' => $match_key['indexvalues']
);
}
}
/* STEP2-4 */
$tables[] = $table;
}
/* STEP4 輸出Markdown,每次循環處理一張表 */
$th = array('字段名', '類型', '空', '默認值', '屬性', '備注');
$th = '|'.implode('|', $th).'|'."\n";
$th .= '|:---:|:---:|:---:|:---:|:---:|:---:|'."\n";
foreach ($tables as $table) {
/* STEP4-1 輸出表結構 */
echo '#'.$table['name'].'('.$table['comment'].')'."\n";
echo $th;
foreach ($table['fields'] as $field) {
$prop_str_arr = '';
$field['primary'] && $prop_str_arr[] = '主鍵';
$field['prop']['auto_increment'] && $prop_str_arr[] = '自增';
$field['prop']['unsigned'] && $prop_str_arr[] = '非標記';
$field['prop']['zerofill'] && $prop_str_arr[] = '非標記并用零填充';
$arr = array($field['primary'] ? '**'.$field['name'].'**' : $field['name'], $field['type'], $field['not_null'] ? '非空' : '可為空', $field['default'], implode(',', $prop_str_arr), $field['comment'], );
echo '|'.implode('|', $arr).'|'."\n";
}
/* STEP4-2 輸出表索引 */
if (!empty($table['index'])) {
$index_th = array('鍵名', '唯一', '字段');
$index_th = '|'.implode('|', $index_th).'|'."\n";
$index_th .= '|:---:|:---:|:---:|'."\n";
echo "\n";
echo "###索引\n";
echo $index_th;
foreach ($table['index'] as $index){
is_array($index['indexvalues']) && $index['indexvalues'] = implode(',', $index['indexvalues']);
$arr = array($index['indexname'], $index['unique'] ? '是' : '否', $index['indexvalues']);
echo '|'.implode('|', $arr).'|'."\n";
//print_r($index);echo "\n";
}
}
echo "\n-------------------------------------------\n\n";
}
exit;
}
```
- 一維數組操作方法
- 求得這個數組中各個元素之和
- 取出兩個數組中相同的內容
- 將數組元素組合為字符串implode
- 將字符串轉化為數組explode
- 逗號分割的字符串去重
- 一維數組增加某個值
- PHP從數組中找到指定元素的位置
- 二維或多維數組操作方法
- 根據某一個鍵值合并生成一個新的二維數組
- 二維數組變一維數組
- 二維數組取一個值變一維數組
- php二維數組合并及去重復的方法
- php數組根據某鍵值,把相同鍵值的合并最終生成一個新的二維數組
- 二維數組增加一個值
- 生成無限級樹算法
- sql2md SQL轉化為markdown語法
- PHP修改多維數組中的某個值
- 二維數組
- 省市區聯動的查詢
- PHP求某二維數組,某一列元素的最大最小值
- php 求二維數組的差集
- 按某個鍵值排序
- 基本知識
- 鍵名作為鍵值
- PHP中foreach的用法和實例
- PHP foreach 循環案例
- php5.3以后的foreach的&符號不建議使用
- PHP中判斷變量為空的幾種方法小結
- 判斷PHP數組是否為空的代碼
- php刪除數組中指定值的元素的幾種方法
- PHP刪除數組中的指定元素
- php數組函數序列之array_keys() - 獲取數組鍵名
- php 中更簡潔的三元運算符 ?:
- 函數
- 字符串函數
- 數組函數
- 實例
- 將 $arr 中age大于第一個大于30的人增加一項一條信息
- 強大的數組函數