$string = "706AN=0,503-PP=0,504-PP=0,519=0,606BOX=0,417=2";
//该变量是数据库列类型文本的结果
我需要单独获取“=”之前的代码以及“=”之后分配给该代码的结果。分割字符串应该返回类似以下内容:
Code 706AN has quantity 0
Code 503-PP has quantity 0
Code 504-PP has quantity 0
Code 519 has quantity 0
Code 606BOX has quantity 0
Code 417 has quantity 2
如何将每个代码与其本身的数量结果相减?
你可以使用爆炸
$string = "706AN=0,503-PP=0,504-PP=0,519=0,606BOX=0,417=2";
$temp = explode(',', $string);
foreach ($temp as $key => $value) {
list($code,$quantity) = explode('=', $value);
echo "Code $code has quantity $quantity </br>";
}
$string = "706AN=0,503-PP=0,504-PP=0,519=0,606BOX=0,417=2";
$exp = explode(',', $string);
foreach ($exp as $key => $value) {
$list = explode('=', $value);
echo "Code $list[0] has quantity $list[1] </br>";
}
您的问题要求似乎不需要在将数据返回到新格式化的字符串之前将文本解析为临时数组。 只需使用
preg_replace()
捕获重要的子字符串并在这些子字符串周围应用所需的文本。 演示
$string = "706AN=0,503-PP=0,504-PP=0,519=0,606BOX=0,417=2";
echo preg_replace(
'#([^=]+)=(\d+),?#',
"Code \$1 has quantity \$2\n",
$string
);
输出:
Code 706AN has quantity 0
Code 503-PP has quantity 0
Code 504-PP has quantity 0
Code 519 has quantity 0
Code 606BOX has quantity 0
Code 417 has quantity 2
稍后我可以给你一个代码示例,我很着急atm
hth