foreach($str as $key=>$row) { if(preg_match_all('/\b[Nn][Ss][Kk]-\w{5}/',$sn,$match)){ $str[$key]['darfon_pn']=substr_replace($match[0][0],$row['darfon_pn'],7,2); }else{ $str[$key]['darfon_pn'] = ''; } }
$str[$key]['darfon_pn']=substr_replace($match[$key][0],$row['darfon_pn'],7,2);
$str[$key]['darfon_pn']=substr_replace($match[0][ $key],$row['darfon_pn'],7,2);
if(preg_match_all('/\b[Nn][Ss][Kk]-\w{5}/',$sn,$match)) { //由于$sn是另外的变量,没有必要放在循环里多次检查 foreach($match[0] as $v) { //对于每一个匹配到的串 foreach($str as $key=>$row) { //修改$str $str[$key]['darfon_pn'] = substr_replace($v, $row['darfon_pn'],7,2); } }}else{ //否则 foreach($str as $key=>$row) { $str[$key]['darfon_pn'] = ''; //赋空值 }}
if(preg_match_all('/\b[Nn][Ss][Kk]-\w{5}/',$sn,$match)) { //由于$sn是另外的变量,没有必要放在循环里多次检查 foreach($match[0] as $v) { //对于每一个匹配到的串 foreach($str as $key=>$row) { //修改$str $str[$key]['darfon_pn'] = substr_replace($v, $row['darfon_pn'],7,2); } }}else{ //否则 foreach($str as $key=>$row) { $str[$key]['darfon_pn'] = ''; //赋空值 }}
最初的 $str[$key]['darfon_pn'] 的内容是什么?
测试代码
$sn = 'NSK-34582 NSK-UUS89 NSK-89234';preg_match_all('/\b[Nn][Ss][Kk]-\w{5}/',$sn,$match);print_r($match[0]);$str = '________';foreach($match[0] as $v) { $str = substr_replace($v, $str,7,2); echo $str,PHP_EOL;}Array
最初的 $str[$key]['darfon_pn'] 的内容是什么?
测试代码
$sn = 'NSK-34582 NSK-UUS89 NSK-89234';preg_match_all('/\b[Nn][Ss][Kk]-\w{5}/',$sn,$match);print_r($match[0]);$str = '________';foreach($match[0] as $v) { $str = substr_replace($v, $str,7,2); echo $str,PHP_EOL;}Array
最初的 $str[$key]['darfon_pn'] 的内容是什么?
测试代码
$sn = 'NSK-34582 NSK-UUS89 NSK-89234';preg_match_all('/\b[Nn][Ss][Kk]-\w{5}/',$sn,$match);print_r($match[0]);$str = '________';foreach($match[0] as $v) { $str = substr_replace($v, $str,7,2); echo $str,PHP_EOL;}Array
明白你的意思了
preg_match_all('/\b[Nn][Ss][Kk]-\w{5}/',$sn,$match);foreach($str as $key=>$row){ if($match[0][0]){ $str[$key]['darfon_pn']=substr_replace($match[0][0],$row['darfon_pn'],7,2); array_shift($match[0]); }else{ $str[$key]['darfon_pn'] = ''; }}无论 $str 有多少项,按匹配出的结果逐一替换,用完了就赋空值