foreach($lines as $val) { $sql .= preg_replace('/#|--/','',$val); } echo $sql;
$lines=file("./mysql.sql");
//过滤掉注释 $sql="";foreach($lines as $line) strpos($line, '#') !== 0 and strpos($line, '--') !== 0 and ($sql.=$line);
preg_replace('/#(*)/r|--(*)/r/','',$val);不知道对不对
这是mysql.sql文件的内容: -- --------------------------- -- 这是一个数据库安装程序 需要的SQL语句文件 -- ..... -- --------------------------# ---------------------------------- # 这是一个文章表 cms_article # ---------------------------------drop table if exists cms_article; create table cms_article( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) ); # ---------------------------------- # 这是一个文章表 cms_hello # ---------------------------------drop table if exists cms_hello; create table cms_hello( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) ); # ---------------------------------- # 这是一个文章表 cms_demo # ---------------------------------drop table if exists cms_demo; create table cms_demo( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) ); # ---------------------------------- # 这是一个文章表 cms_test # ---------------------------------drop table if exists cms_test; create table cms_test( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) ); # ---------------------------------- # 这是一个文章表 cms_wwwww # ---------------------------------drop table if exists cms_wwwww; create table cms_wwwww( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) ); # ---------------------------------- # 这是一个文章表 cms_abcd # ---------------------------------drop table if exists cms_abcd; create table cms_abcd( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) );
<?php $sql = file('sql.sql'); print_r($sql); $out_sql = array(); foreach($sql as $val) { if (!preg_match('/#|--/',$val,$arr) and trim($val)!='') { $out_sql[] = $val; } } print_r($out_sql); ?>Array ( [0] => -- --------------------------- [1] => -- 这是一个数据库安装程序 需要的SQL语句文件 [2] => -- ..... [3] => -- -------------------------- [4] => [5] => # ---------------------------------- [6] => # 这是一个文章表 cms_article [7] => # --------------------------------- [8] => [9] => drop table if exists cms_article; [10] => create table cms_article( [11] => id int unsigned not null auto_increment, [12] => name varchar(50) not null default '', [13] => num int not null default '0', [14] => primary key (id) [15] => ); [16] => [17] => [18] => # ---------------------------------- [19] => # 这是一个文章表 cms_hello [20] => # --------------------------------- [21] => [22] => drop table if exists cms_hello; [23] => create table cms_hello( [24] => id int unsigned not null auto_increment, [25] => name varchar(50) not null default '', [26] => num int not null default '0', [27] => primary key (id) [28] => ); [29] => [30] => [31] => # ---------------------------------- [32] => # 这是一个文章表 cms_demo [33] => # --------------------------------- [34] => [35] => drop table if exists cms_demo; [36] => create table cms_demo( [37] => id int unsigned not null auto_increment, [38] => name varchar(50) not null default '', [39] => num int not null default '0', [40] => primary key (id) [41] => ); [42] => [43] => [44] => # ---------------------------------- [45] => # 这是一个文章表 cms_test [46] => # --------------------------------- [47] => [48] => drop table if exists cms_test; [49] => create table cms_test( [50] => id int unsigned not null auto_increment, [51] => name varchar(50) not null default '', [52] => num int not null default '0', [53] => primary key (id) [54] => ); [55] => [56] => [57] => # ---------------------------------- [58] => # 这是一个文章表 cms_wwwww [59] => # --------------------------------- [60] => [61] => drop table if exists cms_wwwww; [62] => create table cms_wwwww( [63] => id int unsigned not null auto_increment, [64] => name varchar(50) not null default '', [65] => num int not null default '0', [66] => primary key (id) [67] => ); [68] => [69] => [70] => # ---------------------------------- [71] => # 这是一个文章表 cms_abcd [72] => # --------------------------------- [73] => [74] => drop table if exists cms_abcd; [75] => create table cms_abcd( [76] => id int unsigned not null auto_increment, [77] => name varchar(50) not null default '', [78] => num int not null default '0', [79] => primary key (id) [80] => ); ) Array ( [0] => drop table if exists cms_article; [1] => create table cms_article( [2] => id int unsigned not null auto_increment, [3] => name varchar(50) not null default '', [4] => num int not null default '0', [5] => primary key (id) [6] => ); [7] => drop table if exists cms_hello; [8] => create table cms_hello( [9] => id int unsigned not null auto_increment, [10] => name varchar(50) not null default '', [11] => num int not null default '0', [12] => primary key (id) [13] => ); [14] => drop table if exists cms_demo; [15] => create table cms_demo( [16] => id int unsigned not null auto_increment, [17] => name varchar(50) not null default '', [18] => num int not null default '0', [19] => primary key (id) [20] => ); [21] => drop table if exists cms_test; [22] => create table cms_test( [23] => id int unsigned not null auto_increment, [24] => name varchar(50) not null default '', [25] => num int not null default '0', [26] => primary key (id) [27] => ); [28] => drop table if exists cms_wwwww; [29] => create table cms_wwwww( [30] => id int unsigned not null auto_increment, [31] => name varchar(50) not null default '', [32] => num int not null default '0', [33] => primary key (id) [34] => ); [35] => drop table if exists cms_abcd; [36] => create table cms_abcd( [37] => id int unsigned not null auto_increment, [38] => name varchar(50) not null default '', [39] => num int not null default '0', [40] => primary key (id) [41] => ); )
这是你的代码$lines=file("./mysql.sql");
//过滤掉注释 $sql="";foreach($lines as $line){ $sql .= $line[0] == "#" || $line[0].$line[1] == "--" ? '' : $line; } echo $sql;//我只加了这句 这是结果 drop table if exists cms_article; create table cms_article( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) );drop table if exists cms_hello; create table cms_hello( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) );drop table if exists cms_demo; create table cms_demo( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) );drop table if exists cms_test; create table cms_test( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) );drop table if exists cms_wwwww; create table cms_wwwww( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) );drop table if exists cms_abcd; create table cms_abcd( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) ); 我看不出有什么不对的地方
怎么我运行后结果为: -- --------------------------- drop table if exists cms_article; create table cms_article( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) ); drop table if exists cms_hello; create table cms_hello( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) ); drop table if exists cms_demo; create table cms_demo( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) ); drop table if exists cms_test; create table cms_test( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) ); drop table if exists cms_wwwww; create table cms_wwwww( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) ); drop table if exists cms_abcd; create table cms_abcd( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) );
foreach($lines as $val)
{
$sql .= preg_replace('/#|--/','',$val);
}
echo $sql;
//过滤掉注释
$sql="";foreach($lines as $line)
strpos($line, '#') !== 0 and strpos($line, '--') !== 0 and ($sql.=$line);
-- ---------------------------
-- 这是一个数据库安装程序 需要的SQL语句文件
-- .....
-- --------------------------# ----------------------------------
# 这是一个文章表 cms_article
# ---------------------------------drop table if exists cms_article;
create table cms_article(
id int unsigned not null auto_increment,
name varchar(50) not null default '',
num int not null default '0',
primary key (id)
);
# ----------------------------------
# 这是一个文章表 cms_hello
# ---------------------------------drop table if exists cms_hello;
create table cms_hello(
id int unsigned not null auto_increment,
name varchar(50) not null default '',
num int not null default '0',
primary key (id)
);
# ----------------------------------
# 这是一个文章表 cms_demo
# ---------------------------------drop table if exists cms_demo;
create table cms_demo(
id int unsigned not null auto_increment,
name varchar(50) not null default '',
num int not null default '0',
primary key (id)
);
# ----------------------------------
# 这是一个文章表 cms_test
# ---------------------------------drop table if exists cms_test;
create table cms_test(
id int unsigned not null auto_increment,
name varchar(50) not null default '',
num int not null default '0',
primary key (id)
);
# ----------------------------------
# 这是一个文章表 cms_wwwww
# ---------------------------------drop table if exists cms_wwwww;
create table cms_wwwww(
id int unsigned not null auto_increment,
name varchar(50) not null default '',
num int not null default '0',
primary key (id)
);
# ----------------------------------
# 这是一个文章表 cms_abcd
# ---------------------------------drop table if exists cms_abcd;
create table cms_abcd(
id int unsigned not null auto_increment,
name varchar(50) not null default '',
num int not null default '0',
primary key (id)
);
<?php
$sql = file('sql.sql');
print_r($sql);
$out_sql = array();
foreach($sql as $val)
{
if (!preg_match('/#|--/',$val,$arr) and trim($val)!='')
{
$out_sql[] = $val;
}
}
print_r($out_sql);
?>Array
(
[0] => -- --------------------------- [1] => -- 这是一个数据库安装程序 需要的SQL语句文件 [2] => -- ..... [3] => -- -------------------------- [4] => [5] => # ---------------------------------- [6] => # 这是一个文章表 cms_article [7] => # --------------------------------- [8] => [9] => drop table if exists cms_article; [10] => create table cms_article( [11] => id int unsigned not null auto_increment, [12] => name varchar(50) not null default '', [13] => num int not null default '0', [14] => primary key (id) [15] => ); [16] => [17] => [18] => # ---------------------------------- [19] => # 这是一个文章表 cms_hello [20] => # --------------------------------- [21] => [22] => drop table if exists cms_hello; [23] => create table cms_hello( [24] => id int unsigned not null auto_increment, [25] => name varchar(50) not null default '', [26] => num int not null default '0', [27] => primary key (id) [28] => ); [29] => [30] => [31] => # ---------------------------------- [32] => # 这是一个文章表 cms_demo [33] => # --------------------------------- [34] => [35] => drop table if exists cms_demo; [36] => create table cms_demo( [37] => id int unsigned not null auto_increment, [38] => name varchar(50) not null default '', [39] => num int not null default '0', [40] => primary key (id) [41] => ); [42] => [43] => [44] => # ---------------------------------- [45] => # 这是一个文章表 cms_test [46] => # --------------------------------- [47] => [48] => drop table if exists cms_test; [49] => create table cms_test( [50] => id int unsigned not null auto_increment, [51] => name varchar(50) not null default '', [52] => num int not null default '0', [53] => primary key (id) [54] => ); [55] => [56] => [57] => # ---------------------------------- [58] => # 这是一个文章表 cms_wwwww [59] => # --------------------------------- [60] => [61] => drop table if exists cms_wwwww; [62] => create table cms_wwwww( [63] => id int unsigned not null auto_increment, [64] => name varchar(50) not null default '', [65] => num int not null default '0', [66] => primary key (id) [67] => ); [68] => [69] => [70] => # ---------------------------------- [71] => # 这是一个文章表 cms_abcd [72] => # --------------------------------- [73] => [74] => drop table if exists cms_abcd; [75] => create table cms_abcd( [76] => id int unsigned not null auto_increment, [77] => name varchar(50) not null default '', [78] => num int not null default '0', [79] => primary key (id) [80] => );
)
Array
(
[0] => drop table if exists cms_article; [1] => create table cms_article( [2] => id int unsigned not null auto_increment, [3] => name varchar(50) not null default '', [4] => num int not null default '0', [5] => primary key (id) [6] => ); [7] => drop table if exists cms_hello; [8] => create table cms_hello( [9] => id int unsigned not null auto_increment, [10] => name varchar(50) not null default '', [11] => num int not null default '0', [12] => primary key (id) [13] => ); [14] => drop table if exists cms_demo; [15] => create table cms_demo( [16] => id int unsigned not null auto_increment, [17] => name varchar(50) not null default '', [18] => num int not null default '0', [19] => primary key (id) [20] => ); [21] => drop table if exists cms_test; [22] => create table cms_test( [23] => id int unsigned not null auto_increment, [24] => name varchar(50) not null default '', [25] => num int not null default '0', [26] => primary key (id) [27] => ); [28] => drop table if exists cms_wwwww; [29] => create table cms_wwwww( [30] => id int unsigned not null auto_increment, [31] => name varchar(50) not null default '', [32] => num int not null default '0', [33] => primary key (id) [34] => ); [35] => drop table if exists cms_abcd; [36] => create table cms_abcd( [37] => id int unsigned not null auto_increment, [38] => name varchar(50) not null default '', [39] => num int not null default '0', [40] => primary key (id) [41] => );
)
//过滤掉注释
$sql="";foreach($lines as $line){
$sql .= $line[0] == "#" || $line[0].$line[1] == "--" ? '' : $line;
}
echo $sql;//我只加了这句
这是结果
drop table if exists cms_article;
create table cms_article(
id int unsigned not null auto_increment,
name varchar(50) not null default '',
num int not null default '0',
primary key (id)
);drop table if exists cms_hello;
create table cms_hello(
id int unsigned not null auto_increment,
name varchar(50) not null default '',
num int not null default '0',
primary key (id)
);drop table if exists cms_demo;
create table cms_demo(
id int unsigned not null auto_increment,
name varchar(50) not null default '',
num int not null default '0',
primary key (id)
);drop table if exists cms_test;
create table cms_test(
id int unsigned not null auto_increment,
name varchar(50) not null default '',
num int not null default '0',
primary key (id)
);drop table if exists cms_wwwww;
create table cms_wwwww(
id int unsigned not null auto_increment,
name varchar(50) not null default '',
num int not null default '0',
primary key (id)
);drop table if exists cms_abcd;
create table cms_abcd(
id int unsigned not null auto_increment,
name varchar(50) not null default '',
num int not null default '0',
primary key (id)
);
我看不出有什么不对的地方
-- --------------------------- drop table if exists cms_article; create table cms_article( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) ); drop table if exists cms_hello; create table cms_hello( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) ); drop table if exists cms_demo; create table cms_demo( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) ); drop table if exists cms_test; create table cms_test( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) ); drop table if exists cms_wwwww; create table cms_wwwww( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) ); drop table if exists cms_abcd; create table cms_abcd( id int unsigned not null auto_increment, name varchar(50) not null default '', num int not null default '0', primary key (id) );