Following is the text file we need to read:
users
user
uid
name|datatype|string
login|datatype|string
password|datatype|string
The following code reads the file, picks up relevant data and creates and SQL query.
// read file content into an array
$file = file('users.def');
// create an SQL query
$sql = 'create table ' . $file[0] . ' ( '
. $file[2] . ' serial primary key ';
// loop through name|datatype|string
for ($i = 3; $i < count($file); $i++) {
// split by | and store data in an array
$fields = preg_split("/\|/", $file[$i]);
$sql .= ', ' . $fields[0];
// compare string
if (trim($fields[2]) === "string") {
$sql .= ' varchar(100) ';
} else {
$sql .= ' int ';
}
$sql .= ' not null ';
}
$sql .= ');';
print $sql;
file() function converts file contents into an array where each line can be accessed by an index (the line number). Next we split the line on some symbol, space in this case.