基于php,点亮代码生成技能树
<?php $table = ($_GET['action']); /*生成后端*/ $fileName="core/Motherboard.php"; $handle=fopen($fileName,"r");//使用打开模式为r $contents=fread($handle,filesize($fileName));//读为二进制 $contents = preg_replace('/\{table\}/', $table, $contents);//替换后台表名 //查询表里的列 $sql = "select COLUMN_NAME from information_schema.COLUMNS where table_name = :table_name and table_schema = '".$config['dbname']."' "; $data = array( ':table_name'=>$table ); $data = $db->queryAll($sql,$data); foreach ($data as $key=>$value){ $arrays[] = " $".$value['COLUMN_NAME']." = \$_REQUEST['".$value['COLUMN_NAME']."']; "; } $field=implode("",$arrays); $contents = preg_replace('/\{field\}/', $field, $contents);//替换后台列表 foreach ($data as $key=>$value){ $arr[] = " '".$value['COLUMN_NAME']."' => $".$value['COLUMN_NAME'].", "; } $data=implode("",$arr); $contents = preg_replace('/\{data\}/', $data, $contents);//替换后台列表 /*生成后端*/ $ctxtsubmit=$contents; $f=fopen("action/".$_GET['action'].".php", "wb"); //$text=utf8_encode($ctxtsubmit); //先用函数utf8_encode将所需写入的数据变成UTF编码格式。 $text="\xEF\xBB\xBF".$ctxtsubmit; //"\xEF\xBB\xBF",这串字符不可缺少,生成的文件将成为UTF-8格式,否则依然是ANSI格式。 fputs($f, $text); //写入。 fclose($f);