Link to home
Start Free TrialLog in
Avatar of hypervisor
hypervisor

asked on

preg_match with php for credit card info from magnetic stripe reader

Hello, I'm trying to use the following code to extract the data elements from a magnetic stripe credit card (fake information provided, obviously) - the preg_match is correct, but it's not returning results - I'm a little unfamiliar with this function, so any help would be appreciated:

<?php
$creditcard =  "%B0000111122224444^JACKSON/JANE^1201201100001100000000621000000?";
$swipe = "/^(%B)([0-9]{16})(^)([a-zA-Z-s]*)(/)([a-zA-Z-s]*)(^)([0-9]{2})([0-9]{2})(.)*?$/";
  
preg_match($swipe, $creditcard, $matches);

	echo "<br><br>Credit card details<br>";
    $card =  $matches[2];
    $firstname = $matches[6];
    $lastname =  $matches[4];
    $exp = $matches[9]."".$matches[8];
    echo $card;
    echo $firstname;
    echo $lastname;
    echo $exp;
  
?>

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

See if this helps make sense of it.
http://iconoun.com/demo/temp_hypervisor.php
<?php // demo/temp_hypervisor.php

/**
 * http://www.experts-exchange.com/questions/28700531/preg-match-with-php-for-credit-card-info-from-magnetic-stripe-reader.html
 *
 * http://php.net/manual/en/reference.pcre.pattern.syntax.php
 * http://php.net/manual/en/function.preg-quote.php
 */
error_reporting(E_ALL);
echo '<pre>';

// TEST DATA FROM THE POST AT E-E
$creditcard =  "%B0000111122224444^JACKSON/JANE^1201201100001100000000621000000?";

$swipe
= '#'             // REGEX DELIMITER
. '^'             // AT START OF STRING
. '(%B)'          // CAPTURE GROUP FOR STRING LITERAL
. '([0-9]{16})'   // CAPTURE GROUP FOR 16 DIGITS
. '('
. preg_quote('^')
. ')'
. '([a-zA-Z ]*)'  // CAPTURE GROUP FOR LETTERS AND BLANKS
. '(/)'           // CAPTURE GROUP FOR SLASH
. '([a-zA-Z ]*)'  // CAPTURE GROUP FOR LETTERS AND BLANKS
. '('
. preg_quote('^')
. ')'

. '([0-9]{2})'    // CAPTURE GROUP FOR TWO DIGITS
. '([0-9]{2})'    // CAPTURE GROUP FOR TWO DIGITS
. '([0-9]{4})'    // CAPTURE GROUP FOR FOUR DIGITS
. '(.*?)'         // CAPTURE GROUP FOR ANYTHING OR NOTHING
. '$'             // AT THE END OF THE STRING
. '#'             // REGEX DELIMITER
;

preg_match($swipe, $creditcard, $matches);

$map =
[ 'card'  => 2
, 'lname' => 4
, 'fname' => 6
, 'month' => 8
, 'day'   => 9
, 'year'  => 10
]
;

// MAP THE MATCHED DATA TO APPROPRIATE NAMES
foreach ($map as $text => $key)
{
    echo PHP_EOL . "$text: " . $matches[$key];
}
echo PHP_EOL;

// SHOW EVERYTHING REGEX MATCHED
var_dump($matches);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please help me understand if there was something wrong with my solution.  What did you not understand about it?  Did you try the code and find it unworkable for some reason?