0

我有一个带有零件编号和零件描述的字符串。例如

$MyString = "9210009 - text 6420, 5412, 5224, 4416, 4408 - SSL  Upgrade to 2,000  9210020 - ADC factory installed";

我不会只使用 pn 来获取数组。

$pn = array(
    "9210009",
    "9210020",
);

有人能帮我吗 ?谢谢!

4

2 回答 2

1
$MyString = "9210009 - text 6420, 5412, 5224, 4416, 4408 - SSL  Upgrade to 2,000  9210020 - ADC factory installed";

function extractPartNumbers($str, $pnLength = 7) {
    $pn  = array();
    $str = explode(' ', $str);

    foreach ($str as $s) {
        if (is_numeric($s) && strlen($s) == $pnLength) {
            $pn[] = $s;
        }
    }

    return $pn;
}

print_r( extractPartNumbers($MyString) ); // Array ( [0] => 9210009 [1] => 9210020 )
于 2013-03-14T22:30:39.833 回答
0
$pn = array();
$pnbits= explode("", $mystring);

用它把它分成几块

9210009 - 文本 6420、5412、5224、4416、4408 - SSL 升级到 2,000 9210020 - ADC 出厂安装

然后遍历所有部分,如果它符合您的条件,则将其推入数组并随后打印

foreach ($pnbits as $bits) {
    if (is_numeric($bits) && strlen($bits) == 7) {
        array_push($pn,$bits);
    }
}

print_r($pn);
于 2013-03-14T22:34:35.107 回答