function tripal_bulk_loader_regex_tranform_values
2.x tripal_bulk_loader.loader.inc | tripal_bulk_loader_regex_tranform_values($values, $table_data, $line) |
3.x tripal_bulk_loader.loader.inc | tripal_bulk_loader_regex_tranform_values($values, $table_data, $line) |
1.x tripal_bulk_loader.loader.inc | tripal_bulk_loader_regex_tranform_values($values, $table_data, $line) |
Uses a supplied regex to transform spreadsheet values
Parameters
$values: The select/insert values array for the given table
$table_data: The data array for the given table
Related topics
1 call to tripal_bulk_loader_regex_tranform_values()
- process_data_array_for_line in tripal_bulk_loader/
includes/ tripal_bulk_loader.loader.inc - Process the data array for a given line
File
- tripal_bulk_loader/
includes/ tripal_bulk_loader.loader.inc, line 1008 - Handles the actual loading of data.
Code
function tripal_bulk_loader_regex_tranform_values($values, $table_data, $line) {
if (!array_key_exists('regex_transform', $table_data) or
empty($table_data['regex_transform']) or
!array_key_exists('regex_transform', $table_data) or
!is_array($table_data['regex_transform'])) {
return $values;
}
//tripal_core_report_error('T_bulk_loader', TRIPAL_NOTICE,'Regex Transformation:<pre>'.print_r($table_data['regex_transform'], TRUE).'</pre>', array());
foreach ($table_data['regex_transform'] as $field => $regex_array) {
if (!array_key_exists('replace', $regex_array) or
!array_key_exists('pattern', $regex_array) or
!is_array($regex_array['replace'])) {
continue;
}
// Check for <#column:\d+#> notation
// if present replace with that column in the current line
foreach ($regex_array['replace'] as $key => $replace) {
if (preg_match_all('/<#column:(\d+)#>/', $replace, $matches)) {
foreach ($matches[1] as $k => $column_num) {
$replace = preg_replace('/' . $matches[0][$k] . '/', $line[$column_num -1], $replace);
}
$regex_array['replace'][$key] = $replace;
}
}
// do the full replacement
$old_value = $values[$field];
$new_value = preg_replace($regex_array['pattern'], $regex_array['replace'], $old_value);
$values[$field] = $new_value;
if ($values[$field] === '') {
unset($values[$field]);
}
//print 'Now:'.$values[$field]."\n";
}
return $values;
}