-1

I have a decrypted string that has been posted to our site:

$data = "AQ487r3uiufyds.43543534|VISA|4539991602641096|123|12|15|123|USD|AARON|dr4387yicbekusglisdgc||ZYXW|1970|01|31|19070/01/31|NODIRECTION ROAD, 1111|7th floor apt 777|99999|NOWHERE|XX";

I need to take this data and create variables from each section separated by |.

3
  • 3
    I hope that above does not contain a real credit card number! Commented Apr 27, 2013 at 20:05
  • 1
    The pipes and variables don't really have much to do with this. Your question can be summed up as "how do I split a string by a character in PHP?" and putting that straight into Google will get you what you want. Commented Apr 27, 2013 at 20:11
  • No it wasnt a real card number. Commented Apr 27, 2013 at 21:07

4 Answers 4

6

The function you are looking for is explode. It returns an array of strings, each of which is a substring of the second argument formed by splitting it on boundaries formed by the string delimiter (second argument).

$data = "AQ487r3uiufyds.43543534|VISA|4539991602641096|123|12|15|123|USD|AARON|dr4387yicbekusglisdgc||ZYXW|1970|01|31|19070/01/31|NODIRECTION ROAD, 1111|7th floor apt 777|99999|NOWHERE|XX";
foreach (explode('|', $data) as $p)
    echo "$p<br>";

will output:

AQ487r3uiufyds.43543534
VISA
4539991602641096
123
12
15
123
USD
AARON
dr4387yicbekusglisdgc

ZYXW
1970
01
31
19070/01/31
NODIRECTION ROAD, 1111
7th floor apt 777
99999
NOWHERE
XX
Sign up to request clarification or add additional context in comments.

Comments

3

If you know the structure of the string a-priori, and if you want real named variables (not just an array of values), use PHP's list construct (with explode):

$data = "AQ487r3uiufyds.43543534|VISA|4539991602641096";

list($someCode, $cardType, $cardNumber)=explode('|', $data);

var_dump($someCode);
var_dump($cardType);
var_dump($cardNumber);

Then there's no need to store the temporary array (returned by explode) in a variable.

If you need an array of values, just use explode as suggested in other answers.

2 Comments

I know this is probably being pedantic but given that the string appears to have address information in it, and the format of addresses varies (line 1, line 2, blah blah) using an array would probably be better in this case.
@RickBurgess: This design of communication protocol is not optimal anyway...
1
$vars = explode('|', $data);
//echo $vars[0];
//echo $vars[1];
//$card_num = $vars[2];
print_r($vars);

Comments

1

Simply use explode(). It returns an array of strings, which in turn can be assigned to the variables you create according to your need, e.g.:

// explode the data string separated by |
$data_array = explode("|", $data);
// assign variables
$cc_no = $data_array[0];
$cc_comp = $data_array[1];
// and so on ...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.