I didn't like the regex multiline solution posted here for a number of reasons ranging from "it doesn't work even though I generally know what I'm doing with PHP" to "why the hell are you using regexes..." This code, on the other hand, DOES work (admin\controller\extension\modification.php) by adding 'multiline' and 'ignorespaces' options to the non-regex 'search' tag (just set both to 'true' in your XML):
Code: Select all
// Search and replace
if ($operation->getElementsByTagName('search')->item(0)->getAttribute('regex') != 'true') {
// Search
$search = $operation->getElementsByTagName('search')->item(0)->textContent;
$trim = $operation->getElementsByTagName('search')->item(0)->getAttribute('trim');
$index = $operation->getElementsByTagName('search')->item(0)->getAttribute('index');
$multiline = ($operation->getElementsByTagName('search')->item(0)->getAttribute('multiline') == 'true');
$ignorespaces = ($operation->getElementsByTagName('search')->item(0)->getAttribute('ignorespaces') == 'true');
// Trim line if no trim attribute is set or is set to true.
if (!$trim || $trim == 'true') {
$search = trim($search);
}
// Explode the search on newlines if multiline.
if ($multiline) {
$searchlines = explode("\n", $search);
if ($ignorespaces) {
foreach ($searchlines as $num => $line) $searchlines[$num] = preg_replace('/\s/', "", $line);
}
}
// Add
$add = $operation->getElementsByTagName('add')->item(0)->textContent;
$trim = $operation->getElementsByTagName('add')->item(0)->getAttribute('trim');
$position = $operation->getElementsByTagName('add')->item(0)->getAttribute('position');
$offset = $operation->getElementsByTagName('add')->item(0)->getAttribute('offset');
if ($offset == '') {
$offset = 0;
}
// Trim line if is set to true.
if ($trim == 'true') {
$add = trim($add);
}
// Log
$log[] = 'CODE: ' . $search;
// Check if using indexes
if ($index !== '') {
$indexes = explode(',', $index);
} else {
$indexes = array();
}
// Get all the matches
$i = 0;
$lines = explode("\n", $modification[$key]);
for ($line_id = 0; $line_id < count($lines); $line_id++) {
$line = $lines[$line_id];
// Status
$match = false;
// Check to see if the line matches the search code.
if ($multiline) {
$match = true;
foreach ($searchlines as $num => $line2) {
if ($line_id + $num >= count($lines)) {
$match = false;
break;
}
$line3 = $lines[$line_id + $num];
if ($ignorespaces) $line3 = preg_replace('/\s/', "", $line3);
if ($line2 !== $line3) {
$match = false;
break;
}
}
} else if (stripos($line, $search) !== false) {
// If indexes are not used then just set the found status to true.
if (!$indexes) {
$match = true;
} elseif (in_array($i, $indexes)) {
$match = true;
}
$i++;
}
// Now for replacing or adding to the matched elements
if ($match) {
switch ($position) {
default:
case 'replace':
$new_lines = explode("\n", $add);
if ($offset < 0) {
array_splice($lines, $line_id + $offset, abs($offset) + ($multiline ? count($searchlines) : 1), array(str_replace($search, $add, $line)));
$line_id -= $offset;
} else {
array_splice($lines, $line_id, $offset + ($multiline ? count($searchlines) : 1), array(str_replace($search, $add, $line)));
}
break;
case 'before':
$new_lines = explode("\n", $add);
array_splice($lines, $line_id - $offset, 0, $new_lines);
$line_id += count($new_lines);
break;
case 'after':
$new_lines = explode("\n", $add);
array_splice($lines, ($line_id + ($multiline ? count($searchlines) : 1)) + $offset, 0, $new_lines);
$line_id += count($new_lines);
break;
}
// Log
$log[] = 'LINE: ' . $line_id;
$status = true;
}
}
$modification[$key] = implode("\n", $lines);
} else {
If the OC dev isn't going to drop OCMOD, at least do this instead. It's more sane and makes my head hurt less.