• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to mask credit card number (using regex?)

#11
[eluser]Pygon[/eluser]
The regex I gave is not superfluos[sic] as you put it. In the context as I have put it, with the intention of creating standards, it is not superfluous as it does what is intended. Maybe you feel I should write out an entire library, but I didn't believe it was necessary. Superfluous would be your replacement of letters and and symbols as well as numbers, except "- ", the addition of yet another regex to verify the user input, and yet another regex to strip all of the unnecessary jumble yet again before submission to most billing systems--thus the true meaning of being "redundant".

You are right about "what the original poster asked for..." -- I'm glad the intention of the forum is to obscurely answer questions without providing any insight into other, more useful ways of doing something (which, by the way, I did in my original post with function 2 while increasing the speed of the function and working for 14-16 digit cards).

Atleast you did get one thing right, I didn't write my function to handle AMEX cards, then again, I'm not trying to write a fully functional for every situation system.

FYI, your function is 100% slower even when I correct mine for an strlen of the stripped input. Wink As fun as point, counter-point is, I think coolfactors question has been answered.

#12
[eluser]untermensch[/eluser]
[quote author="Pygon" date="1194046367"]The regex I gave is not superfluos[sic] as you put it[/quote]

Like I said, try running it. All you're doing with that is stripping off the first character of the line if it happens to be a number. You *probably* meant to strip out all non-digit chars, but that would change the format of the credit card, and the poster clearly wanted something in the form of XXXX-XXXX-XXXX-1234.

Quote:Superfluous would be your replacement of letters and and symbols as well as numbers, except "- ", the addition of yet another regex to verify the user input

Hello there! That's *exactly* what the poster asked for! Your regex *attempted* to strip input, which is redundant considering the point about validation and considering coolfactor asked not to assume the number should be in any specific format. The regex you call redundant is the entire business end of the solution.

Quote:FYI, your function is 100% slower even when I correct mine for an strlen of the stripped input. Wink

Ok, this is a really laughable point. Rule #1 of Optimisation: optimise where it will make a difference. If you are masking millions of CC numbers per minute, fair enough; I'd also look to optimise - by writing the bastard in C!

But that's an entirely ridiculous thing to be doing in this context, and I'm talking as someone who spent years handcoding machine code to get the fastest inner loops on various games consoles, and embedded systems.

(besides, my benchmarks show me mine is actually 38% slower)

#13
[eluser]Pygon[/eluser]
I would think it better to impart different ideas and concepts rather than taking your "just answer the posters question" view so that others could learn and develop their own, better, solutions. Maybe you just want to be right?

#14
[eluser]untermensch[/eluser]
[quote author="Pygon" date="1194049523"]I would think it better to impart different ideas and concepts rather than taking your "just answer the posters question" view so that others could learn and develop their own, better, solutions. Maybe you just want to be right?[/quote]
Clutching at straws now, really. Nobody mentioned right or wrong here. As much as you'd like to think this thread was imparting different ideas and concepts, the poster wanted a simple answer to a simple question, and there's been a fairly poor signal:noise ratio since then.

coolfactor, if you're still here:

You wanted to know how to implement the same regex from Ruby in PHP. To answer that question, you have to do some regex learning, and find that the Ruby regex implementation is more flexible than PCRE in PHP. You have more powerful back-reference operators (eg, $1.length) and the char multiply, therefore allowing the Ruby solution to appear more elegent.

However, I'm not sure PHP gives you anything like "$1.length" directly, which is what's needed to be able to strip off or duplicate a certain number of characters easily in the regex's replace clause. Instead you have to fudge around with substr() or whatever you choose, in order to obtain the same effect - or write a horrendous looking regex to try and perform the same task.

#15
[eluser]Pygon[/eluser]
Might want to note that:
Code:
@card_masked = card_masked.sub(/^([0-9]+)([0-9]{4})$/) { '*' * $1.length + $2 }

Doesn't create "xxxx-xxxx-xxxx-1234" either. Like my function, it creates "************1234" and expects the input to be only numbers. Don't know if you noticed that coolfactor.

#16
[eluser]Rick Jolly[/eluser]
[quote author="untermensch" date="1194050235"][quote author="Pygon" date="1194049523"]I would think it better to impart different ideas and concepts rather than taking your "just answer the posters question" view so that others could learn and develop their own, better, solutions. Maybe you just want to be right?[/quote]
Clutching at straws now, really. Nobody mentioned right or wrong here. As much as you'd like to think this thread was imparting different ideas and concepts, the poster wanted a simple answer to a simple question, and there's been a fairly poor signal:noise ratio since then.
[/quote]
Well put untermensch. Coolfactor's question was very simple and clear, and it deserved an equally lucid, accurate answer. Like the internet in general, there is a lot of fluff floating around these forums.

#17
[eluser]Pygon[/eluser]
[quote author="Rick Jolly" date="1194053735"]
Well put untermensch. Coolfactor's question was very simple and clear, and it deserved an equally lucid, accurate answer. Like the internet in general, there is a lot of fluff floating around these forums.[/quote]

I see issues with literacy or reading comprehension abound; the question had been answered before any further discussion began.

#18
[eluser]esra[/eluser]
Something that I noted down in my journal last May for future reference as cc_help.php but have not used it myself. It was posted on a blog elsewhere but the blog no longer exists. According to my notes, this should result in a mask like xxxx-xxxx-xxxx-1234

Code:
<?php
function mask ( $str, $start = 0, $length = null ) {
    $mask = preg_replace ( "/\S/", "*", $str );
    if ( is_null ( $length )) {
        $mask = substr ( $mask, $start );
        $str = substr_replace ( $str, $mask, $start );
    } else {
        $mask = substr ( $mask, $start, $length );
        $str = substr_replace ( $str, $mask, $start, $length );
    }
    return $str;
}
?>

#19
[eluser]Rick Jolly[/eluser]
[quote author="Pygon" date="1194115445"][quote author="Rick Jolly" date="1194053735"]
Well put untermensch. Coolfactor's question was very simple and clear, and it deserved an equally lucid, accurate answer. Like the internet in general, there is a lot of fluff floating around these forums.[/quote]

I see issues with literacy or reading comprehension abound; the question had been answered before any further discussion began.[/quote]

Yea, I'm illiterate.

The question had been answered before you entered this thread and "imparted different ideas". Coolfactor clearly stated that the solution must not make any assumptions about format. Instead, you touted "standards" as if credit card numbers have standards. As if every credit card number has 16 digits.

I'm not suggesting that we shouldn't throw ideas around. Everyone has something of value to add. I'm just a bit tired of stubborn posters arguing their questionable solutions to death. A little humility please.

#20
[eluser]Pygon[/eluser]
Yea, I'm illiterate.
Your resulting statement would directly call into question your understanding of the text.

The question had been answered before you entered this thread and "imparted different ideas".
Then I suspect you're adding to the "fluff" as well.

Coolfactor clearly stated that the solution must not make any assumptions about format.
No, coolfactor clearly stated that HIS solution did not make any assumptions. I believe that falls under that reading comprehension thing.

Instead, you touted "standards" as if credit card numbers have standards. As if every credit card number has 16 digits.
I "touted" standardizing the card format within the billing system, eliminating the need to rely on REGEX every time the card was processed. The code was never intended as an "end-all, be-all" solution, but did its purpose for what he DID ask for, "XXXX-XXXX-XXXX-1234" (16 digits if there is any kind of counting problem too).


I'm not suggesting that we shouldn't throw ideas around. Everyone has something of value to add. I'm just a bit tired of stubborn posters arguing their questionable solutions to death. A little humility please.
You apparently have comprehension issues with your own writing as well, since you clearly state that:
Coolfactor’s question was very simple and clear, and it deserved an equally lucid, accurate answer. Like the internet in general, there is a lot of fluff floating around these forums.
I'll help you out -- you imply that other "ideas" are equivilent to "fluff", as evident by the second quote above.

I was perfectly fine discussing "over-engineering" and the like until you and your cohort decided it was more important to introduce this "fluff" you speak of rather than discussing the topic of masking credit cards.

Here's some humility for your "everyone else is the problem" attitude:

I appologize for derailing your topic CF; I'll let others continue the fluff from here on.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.