[Solved] Syntax problem with sgapiPreg_Replace

0

I am capturing phone numbers in my survey and respondents can enter most anything that resembles a North American phone number and it passes the RegEx validation.  For example, phone numbers might be 202-555-1212 or (202) 555-1212 or 202 555 1212, etc.  I would like to strip out all the non-numeric characters and just be left with a 10-digit phone number.

sgapiPreg_Replace syntax follows this format: sgapiPreg_Replace(%%subject, %%pattern, %%replacement, %%limit)

I’m using this code and I’m not getting an error but I’m not getting any results returned.

%%subject = “(202) 555-1212”;
%%pattern = “/[^0-9]/”;
%%replacement = “”;
%%limit = -1
%%phoneNumber2 = sgapiPreg_Replace(%%subject, %%pattern, %%replacement, %%limit);

I suspect the problem is with %%replacement but after reviewing numerous online sources, this seems to be correct.

Any ideas on what I should do to get this function working properly or how I could use a different approach to strip out the non-numeric characters?

Patrick McCabe asked

    Best answer

    1

    Hi Patrick –

    You’re using the right approach! There are just a few things that need to be fixed.

    The line that sets the limit is missing a semi-colon.

    The order of the arguments in sgapiPreg_Replace is wrong. Sorry! That’s our fault. The documentation listed the wrong order. I just updated it.

    This should work:

    %%subject = “(202) 555-1212”;
    %%pattern = “/[^0-9]/”;
    %%replacement = “”;
    %%limit = -1;
    %%phoneNumber2 = sgapiPreg_Replace(%%pattern, %%replacement, %%subject, %%limit);

    It looks like the forum is replacing the normal quotes with ‘smart’ quotes. Make sure to replace them with normal quotes if you and copy and paste any code from the community.

    Best,

    Zach

    Zachary answered
      ×

      Login

      Question stats

      • Active
      • Views1284 times
      • Answers1 answer
      • Followers0 followers