Projet

Général

Profil

Paste
Télécharger (2,03 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds_tamper / plugins / find_replace_regex.inc @ 7b9e8704

1
<?php
2

    
3
/**
4
 * @file
5
 * Find/replace text using REGEX.
6
 */
7

    
8
$plugin = array(
9
  'form'     => 'feeds_tamper_find_replace_regex_form',
10
  'callback' => 'feeds_tamper_find_replace_regex_callback',
11
  'validate' => 'feeds_tamper_find_replace_regex_validate',
12
  'name'     => 'Find replace REGEX',
13
  'multi'    => 'direct',
14
  'category' => 'Text',
15
);
16

    
17
function feeds_tamper_find_replace_regex_form($importer, $element_key, $settings) {
18
  $form = array();
19

    
20
  $form['find'] = array(
21
    '#type' => 'textfield',
22
    '#title' => t('REGEX to find'),
23
    '#default_value' => isset($settings['find']) ? $settings['find'] : '',
24
    '#description' => t('A regular expression in the form: @regex', array('@regex' => '/<your regex here>/')),
25
    '#maxlength' => 1024,
26
  );
27

    
28
  $form['replace'] = array(
29
    '#type' => 'textfield',
30
    '#title' => t('Replacement pattern'),
31
    '#default_value' => isset($settings['replace']) ? $settings['replace'] : '',
32
    '#description' => t('The replacement pattern.'),
33
    '#maxlength' => 1024,
34
  );
35

    
36
  $form['limit'] = array(
37
    '#type' => 'textfield',
38
    '#title' => t('Limit number of replacements'),
39
    '#default_value' => isset($settings['limit']) ? $settings['limit'] : '',
40
    '#description' => t('This sets an optional limit. Leave it blank for no limit.'),
41
  );
42

    
43
  return $form;
44
}
45

    
46
function feeds_tamper_find_replace_regex_validate(&$settings) {
47
  // Test the regex.
48
  $test = @preg_replace($settings['find'], '', 'asdfsadf');
49
  if ($test === NULL) {
50
    form_set_error('settings][find', 'Invalid regular expression.');
51
  }
52

    
53
  $settings['limit'] = trim($settings['limit']);
54

    
55
  if (empty($settings['limit'])) {
56
    $settings['real_limit'] = -1;
57
  }
58
  else {
59
    $settings['real_limit'] = $settings['limit'];
60
  }
61
  if (!is_numeric($settings['real_limit'])) {
62
    form_set_error('settings][limit', 'Limit must be an integer.');
63
  }
64
}
65

    
66
function feeds_tamper_find_replace_regex_callback($result, $item_key, $element_key, &$field, $settings, $source) {
67
  $field = preg_replace($settings['find'], $settings['replace'], $field, $settings['real_limit']);
68
}