解析管道分隔的字符串[重复]

This question already has an answer here:

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 |.

</div>

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
$vars = explode('|', $data);
//echo $vars[0];
//echo $vars[1];
//$card_num = $vars[2];
print_r($vars);

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 ...

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.