Professional Web Applications Themes

substr parsing mask - PERL Beginners

Does anyone have a short routine for displaying mask on some values and displaying the value of the last four? For example, alot of site display credit card numbers like x1234 which shows only the last four. I know how to use the substr but what about replacing the preceding values with x? thanks, -rkl...

  1. #1

    Default substr parsing mask

    Does anyone have a short routine for displaying mask on some values and
    displaying the value of the last four? For example, alot of site display
    credit card numbers like x1234 which shows only the last four.

    I know how to use the substr but what about replacing the preceding values
    with x?

    thanks,
    -rkl
    perl@swanmail.com Guest

  2. #2

    Default Re: substr parsing mask

    On Sep 26, [email]perlswanmail.com[/email] said:
    >Does anyone have a short routine for displaying mask on some values and
    >displaying the value of the last four? For example, alot of site display
    >credit card numbers like x1234 which shows only the last four.
    >
    >I know how to use the substr but what about replacing the preceding values
    >with x?
    You could use a regex:

    $string = "1234567890";
    $string =~ s/.(?=.{4})/x/g;

    That means "for each character that is followed by four characters (at
    least), replace it with an 'x'". That means that $string will become
    "7890". You could also use substr():

    substr($string, 0, -4) = "x" x (length($string) - 4);

    The right-hand side makes a string of x's as long as the string is, minus
    four. So for "1234567890", which has length 10, the right-hand side is a
    string of 6 x's, "". The left-hand side gets a substring of $string
    starting at the beginning and ending four characters from the end. We can
    CHANGE the return value of substr():

    $name = "jeff";
    substr($name, 0, 1) = "J";
    print $name; # Jeff

    This means we change all but the last 4 characters of $string to a string
    of x's as long as that beginning chunk of string.

    You can also put the replacement value as the fourth argument to the
    substr() function.

    substr($string, 0, -4, "x" x (length($string) - 4));

    --
    Jeff "japhy" Pinyan [email]japhypobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    [ I'm looking for programming work. If you like my work, let me know. ]

    Jeff 'Japhy' Pinyan Guest

  3. #3

    Default Re: substr parsing mask

    On Sep 27, Jeff 'japhy' Pinyan said:
    >On Sep 26, [email]perlswanmail.com[/email] said:
    >
    >>Does anyone have a short routine for displaying mask on some values and
    >>displaying the value of the last four? For example, alot of site display
    >>credit card numbers like x1234 which shows only the last four.
    >>
    >>I know how to use the substr but what about replacing the preceding values
    >>with x?
    >
    >You could use a regex:
    >
    > $string = "1234567890";
    > $string =~ s/.(?=.{4})/x/g;
    You could also use:

    $string =~ s/(.*)(.{4})/"x" x length($1) . $2/e;

    It matches all but the last four characters to $1, and those last four
    characters go into $2. Then the right-hand side is evaluated as code
    (because of the /e modifier on the regex). We get an "x" for each
    character in $1, and then just tack on $2.

    --
    Jeff "japhy" Pinyan [email]japhypobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    [ I'm looking for programming work. If you like my work, let me know. ]

    Jeff 'Japhy' Pinyan Guest

  4. #4

    Default Re: substr parsing mask

    I got it to work after adding a new var in apache modperl environment.

    This works:
    $acct_no = $xgi->param("acct_no");
    print substr($acct_no, 0, 4, "x" x (length($acct_no)-4));

    This did NOT work:
    print substr($xgi->param("acct_no"), length($xgi->param("acct_no"))-4,4);

    Any explanation would be great.
    -rkl

    I tried to save a line by using
    > On Sep 27, [email]perlswanmail.com[/email] said:
    >
    >>> substr($string, 0, -4, "x" x (length($string) - 4));
    >>
    >>I couldn't get this to work correctly. it only returns the 4 characters
    >> of
    >>the string whcih is correct. But it did not replace the preceding
    >>characters with x.
    >
    > It works for me:
    >
    > pinyajrcs-sgi [11:47am] ~ #103> perl -l
    > $string = join '', .. 9;
    > substr($string, 0, -4, "x" x (length($string) - 4));
    > print $string;
    > __END__
    > 6789
    >
    > --
    > Jeff "japhy" Pinyan [email]japhypobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    > RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    > <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    > [ I'm looking for programming work. If you like my work, let me know. ]
    >
    >
    perl@swanmail.com Guest

  5. #5

    Default Re: substr parsing mask

    On Sep 29, [email]perlswanmail.com[/email] said:
    >I got it to work after adding a new var in apache modperl environment.
    >
    >This works:
    > $acct_no = $xgi->param("acct_no");
    > print substr($acct_no, 0, 4, "x" x (length($acct_no)-4));
    >
    >This did NOT work:
    > print substr($xgi->param("acct_no"), length($xgi->param("acct_no"))-4,4);
    They're not the same code.

    print substr(
    $xgi->param('acct_no'),
    0,
    4,
    "x" x (length($xgi->param('acct_no')) - 4)
    );

    --
    Jeff "japhy" Pinyan [email]japhypobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    [ I'm looking for programming work. If you like my work, let me know. ]

    Jeff 'Japhy' Pinyan Guest

Similar Threads

  1. #40754 [NEW]: substr() checks overflow
    By christopher dot jones at oracle dot com in forum PHP Bugs
    Replies: 2
    Last Post: March 9th, 02:00 AM
  2. Help translating fread to substr
    By RootShell in forum PHP Development
    Replies: 1
    Last Post: May 4th, 10:02 PM
  3. [PHP-DEV] [PATCH] substr() returns false
    By Morten Poulsen in forum PHP Development
    Replies: 2
    Last Post: November 20th, 10:04 AM
  4. [PHP] substr ?
    By John Taylor-Johnston in forum PHP Development
    Replies: 0
    Last Post: August 17th, 05:49 AM
  5. InStr, substr, Mid ???
    By J. Muenchbourg in forum ASP
    Replies: 1
    Last Post: August 13th, 12:18 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139