1

Hello Drupal community,

I'm finding some issues in a tamper used in feed's module. I want spaces to be deleted in a field that contains numbers. Something that changes "00 00 00 00 00" into "0000000000". I've tried the following :

    Regex to find : #(\d{2})[ ](\d{2})[ ](\d{2})[ ](\d{2})[ ](\d{2})#
    Replacement pattern : $1$2$3$4$5

enter image description here

Note that the expression was validated in https://regex101.com/ : enter image description here

But it doesn't seem to work. Anyone please ?

  • 1
    try str_replace(" ", "", $string); or preg_replace("/ +/", "", $string); – Bipin K Aug 13 '15 at 10:25
  • @BipinK Actually, i'm not custom coding it. It's part of feed's tamper module. I just added the imagine to illustrate the problem. Thanks for your help ! – Ismail H Aug 13 '15 at 11:59
  • 1
    @IsmaH No matter if you are custom coding it or not - it is good idea to test if your regexp is valid in the first place. At the moment I see nothing like that in your question. See here if you don't have easy access to php-enabled shell. – Mołot Aug 13 '15 at 12:01
  • @Mołot I've already tested the pattern in regex101.com and it's working. I will change my post accordingly. Thank you. – Ismail H Aug 13 '15 at 12:30
1

What you need to do is something like this

#(\d{2})\s+#

and then replace with $1

then if you need to limit it, use the limit field.

Its better to try and match the pattern thank the whole string- that one matches 2 numbers, followed by at least one space, and replaces it with the captured number (in brackets) - so it'll match the whole thing.

you could add a positive look ahead (?\d) to make sure it matches 2 numbers then space followed by a number, but testing the above worked

| improve this answer | |
1

Try giving Limit number of replacements value as -1 This will solve your issue.

| improve this answer | |
  • Not working :( :( Sorry – Ismail H Aug 13 '15 at 12:34
1

Not a solution to this exact issue, but a troubleshooting guide:

What was helpful to me was to dpm the module's parameters and variables. In this file: \sites\all\modules\feeds_tamper\plugins\find_replace_regex.inc - modify this code to insert dmp() functions. (you'll need the devel module for this)

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

Then go to your importer page /import and run the import you've created.

Regex101 doesn't emulate php exactly from my personal experience. My tests would return "" in conflict with Regex101. For a quick php-test use: http://phptester.net/

like this:

$string = 'echoMyString';
$regex = '/(echo)(MyString)/';
$replace = '$2';

echo preg_replace($regex, $replace, $string, '-1');

[Output:] MyString

This procedure helped me troubleshoot why my regex wasn't working for Feeds Tamper Importer

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.