On Sep 26, [EMAIL PROTECTED] 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 xxxx1234 which shows only the last four.
>
>I know how to use the substr but what about replacing the preceding values
>with xxxx?

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
"xxxxxx7890".  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, "xxxxxx".  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 PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
<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.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to