如何从'^'分隔列表中获取产品ID?

Jon*_*ein -3 php regex

我有一个'^'分隔的产品ID号列表,我需要只获取产品ID号,然后用它来查询SQL数据库.产品ID号存储在$ _SESSION哈希中.例如:

SKUS: jpn18726^gr172645^123746^17246^eu186726^...

我能想到的代码是这样的:

$prodmat = $_SESSION["product"];
if(preg_match("(\d+)(^\s*\d+)*", $prodmat) {
    $stmt = "select shipcode from materials where material='???'";
}
Run Code Online (Sandbox Code Playgroud)

基本上,我想从'^'分隔列表中提取产品ID号,然后使用产品ID号查询DB.

Jay*_*ard 6

做一些爆炸:

$prod_list = 'SKUS: jpn18726^gr172645^123746^17246^eu186726';
$list_parts = explode(':', $prod_list); // separate the text
$prods = explode('^', trim($list_parts[1])); // trim and put the list in an array
print_r($prods);
Run Code Online (Sandbox Code Playgroud)

结果:

Array
(
    [0] => jpn18726
    [1] => gr172645
    [2] => 123746
    [3] => 17246
    [4] => eu186726
)
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用查询遍历数组.

foreach($prods as $product) {
     $sql = "SELECT foo, bar, WHERE products WHERE id = ?";
     // bind the current product
     // do the query
}
Run Code Online (Sandbox Code Playgroud)