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

hypervisorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
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

0
hieloCommented:
Try:
$creditcard =  "%B0000111122224444^JACKSON/JANE^1201201100001100000000621000000?";

$swipe = '/^\D+(\d+)\x5e([^\/]+)\/([^\x5e]+)\x5e(\d{2})(\d{2})(\d+)./';
preg_match($swipe, $creditcard, $matches);

// "inspect" what was captured
echo '<pre>',print_r($matches,true),'</pre>';

    echo "<br><br>Credit card details<br>";
    $card =  $matches[1];
    $firstname = $matches[2];
    $lastname =  $matches[3];
    $exp = $matches[4]."".$matches[5];
    echo $card;
    echo $firstname;
    echo $lastname;
    echo $exp;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
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?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.