Forum Moderators: coopster

Message Too Old, No Replies

Parsing curly braces { }

         

coaster_vn

5:02 am on Jun 4, 2005 (gmt 0)

10+ Year Member



Hey guys, first post here. I have a question. Basically, I have a flat file that comes in. The format is basically as follows:

{FEATURE} description description {FEATURE}

The choices are delimited by 2 spaces. The items between the curly's are first checked to see if they are an actual column in the database. If they are not a column, add them to another column called features. Also depending on the feature, the descriptions would either have 1 or many.

My main question is, how would i parse the {feature} to determine whether or not it is a db column, and how would i store the descriptions accordingly.

Thanks in advance.

coopster

2:57 pm on Jun 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, coaster_vn.

One option would be to DESCRIBE the table to get the column names and load them into an array which you would check your {COLUMN_NAME} against. I would use a regular expression to parse the flat file rows(s).

Side note:
-------------
It seems kind of useless to have the second {COLUMN_NAME} at the end of each row in your flat file, a waste of storage somewhat. Well, hold on. I guess I am assuming there is a newline at the end of each flat file row that you are reading in. If not, you will want to modify the regular expression in the following code snippet.

$table_cols = array(); // initialze 
$table = 'products';
$rows = mysql_query("DESCRIBE $table");
while ($row = mysql_fetch_array($rows)) {
$table_cols[] = $row['Field'];
// uncomment next line to see what comes back
// from a MySQL DESCRIBE statement:
//print '<pre>'; print_r($row); print '</pre>';
}
$flat_file_row = '{FEATURE} description description {FEATURE}';
preg_match [php.net]('/{(.*)}([^{]*){/', $flat_file_row, $matches);
//print '<pre>'; print_r [php.net]($matches); print '</pre>'
$column_name = $matches[1];
//print '<pre>'; print $column_name; print '</pre>'
$values = trim [php.net]($matches[2]);
//print '<pre>'; print $values; print '</pre>'
if (in_array [php.net]($column_name, $table_cols)) {
// Insert/Update your database $column_name column
} else {
// Insert/Update your database FEATURE column
}
exit;

Uncomment any print lines to see what is coming back in the browser. Lots to learn in this little snippet, so take some time to analyze what is going on. Any questions, let us know.