Page 1 of 1

how to search multiple lines in OCMOD

Posted: Tue Mar 03, 2015 8:11 pm
by mujji89
I have to search this code in ocmod and replace with another
My current search and add tags

Code: Select all

<search><![CDATA[
		 		<input type="text" name="price" value="<?php echo $price; ?>" placeholder="<?php echo $entry_price; ?>" id="input-price" class="form-control" />
		 		</div>
		 		</div>
              ]]></search>
			<add position="replace" ><![CDATA[
				test data
			]]></add>
The problem is it doesn't searches it. It has to do something with multiple line search. For single line it works perfect

Can anyone help?

Re: how to search multiple lines in OCMOD

Posted: Fri Mar 06, 2015 9:22 am
by gecko_gp
Same problem for:

Code: Select all

    <file path="catalog/model/checkout/order.php">
        <operation>
            <search><![CDATA[
					foreach ($emails as $email) {
						if ($email && preg_match('/^[^\@]+@.*.[a-z]{2,15}$/i', $email)) {
							$mail->setTo($email);
							$mail->send();
						}
					}
				}
            ]]></search>

Re: how to search multiple lines in OCMOD

Posted: Sat Mar 07, 2015 12:31 am
by gecko_gp
same problem here :(

Re: how to search multiple lines in OCMOD

Posted: Tue May 12, 2015 2:09 am
by RuslanBrest

Re: how to search multiple lines in OCMOD

Posted: Tue Sep 01, 2015 5:22 am
by maxsmith
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.