> $vals * @return array{head:list,rows:array>,top:string}|null */ public static function parse(array $vals): ?array { $h = null; foreach ($vals as $i => $r) { if (is_array($r) && $r !== [] && in_array(str_replace(' ', '', trim(self::text($r[0] ?? null))), self::HEADER_FIRST_CELLS, true)) { $h = $i; break; } } if ($h === null) { return null; } $ncol = 0; foreach ($vals as $r) { $ncol = max($ncol, is_array($r) ? count($r) : 0); } $head = []; for ($i = 0; $i < $ncol; $i++) { $head[] = str_replace("\n", ' ', self::text($vals[$h][$i] ?? null)); } $start = $h + 1; $next = $vals[$h + 1] ?? []; $c0 = is_array($next) && $next !== [] ? trim(self::text($next[0] ?? null)) : ''; if (is_array($next) && $next !== [] && in_array($c0, ['', '省份'], true) && self::anyTruthy(array_slice($next, 1))) { for ($i = 0; $i < $ncol; $i++) { $head[$i] = $head[$i] . ' ' . self::text($next[$i] ?? null); } $start = $h + 2; } $rows = []; foreach (array_slice($vals, $start) as $r) { if (! is_array($r) || $r === [] || ($r[0] ?? null) === null) { continue; } $name = (string) preg_replace('/\s/u', '', self::rawText($r[0])); if ($name === '' || mb_strlen($name) > 8) { continue; } $key = in_array($name, ['深圳', '舟山', '拉萨'], true) ? $name : self::province($name); if (! array_key_exists($key, $rows)) { $row = []; for ($i = 0; $i < $ncol; $i++) { $row[] = self::num($r[$i] ?? null); } $rows[$key] = $row; } foreach (self::CITY_KEYS as $ck) { // 城市单独报价行(如 深圳市 / 深圳广州) if (str_contains($name, $ck) && $ck !== $key && ! array_key_exists($ck, $rows)) { $rows[$ck] = $rows[$key]; } } } $top = implode(' ', array_map(static fn ($c) => self::text($c), is_array($vals[0] ?? null) ? $vals[0] : [])); return ['head' => $head, 'rows' => $rows, 'top' => $top]; } /** 第一个匹配任一正则、且不匹配排除正则的表头列号(从 $start 起)。 */ public static function col(array $head, array $patterns, int $start = 1, array $exclude = []): ?int { for ($i = $start, $n = count($head); $i < $n; $i++) { $t = $head[$i]; $hit = false; foreach ($patterns as $p) { if (preg_match('/' . $p . '/u', $t)) { $hit = true; break; } } if (! $hit) { continue; } foreach ($exclude as $e) { if (preg_match('/' . $e . '/u', $t)) { continue 2; } } return $i; } return null; } /** * 表头里的重量段:[下限, 上限, 列号]。识别 0-1kg、500g-1kg、300g、3kg、一口价 等写法。 * * @param iterable $cols * @return list */ public static function bands(array $head, iterable $cols): array { $out = []; foreach ($cols as $i) { $t = str_replace([' ', '~', '~'], ['', '-', '-'], (string) ($head[$i] ?? '')); if (preg_match('/(\d+)g-([\d.]+)kg/iu', $t, $m)) { $out[] = [(float) $m[1] / 1000, (float) $m[2], $i]; } elseif (preg_match('/^(\d+)g(一口价)?$/iu', $t, $m)) { $out[] = [0.0, (float) $m[1] / 1000, $i]; } elseif (preg_match('/([\d.]+)\s*[-~]\s*([\d.]+)/u', $t, $m) && is_numeric($m[1]) && is_numeric($m[2])) { $out[] = [(float) $m[1], (float) $m[2], $i]; } elseif (preg_match('/([\d.]+)\s*(kg|公斤|KG)/u', $t, $m) && is_numeric($m[1])) { $out[] = [0.0, (float) $m[1], $i]; } elseif (str_contains($t, '一口价')) { $out[] = [0.0, 1e9, $i]; } } return $out; } /** 文字里出现的省份(两字简称)。 */ public static function provincesIn(mixed $text): array { $t = (string) preg_replace('/\s/u', '', self::text($text)); $out = []; foreach (self::PROVINCES as $p) { if (str_contains($t, $p)) { $out[$p] = true; } } return $out; } /** * 按条码报价的表:{条码: [[省份集合|null(其余), 价格]]}、不发省份、布局。 * * @return array{0:array>,1:array,2:string} */ public static function barcodeTable(array $vals): array { $table = []; $nogo = []; foreach ($vals as $r) { foreach ((array) $r as $c) { $s = self::text($c); if ($s !== '' && str_contains($s, '不发') && ! preg_match(self::BARCODE_RE, $s)) { $nogo += self::provincesIn($c); } } } $isBarcode = static fn ($c): bool => (bool) preg_match('/^' . trim(self::BARCODE_RE, '/u') . '$/u', trim(self::text($c))); $rowsBc = array_values(array_filter($vals, static fn ($r) => is_array($r) && ($r[0] ?? null) && $isBarcode($r[0]))); if ($rowsBc !== []) { $hdr = null; foreach ($vals as $r) { foreach ((array) $r as $c) { $s = self::text($c); if ($s !== '' && (str_contains($s, '剩余') || str_contains($s, '其余') || str_contains($s, '其他'))) { $hdr = $r; break 2; } } } $regions = []; if ($hdr !== null) { foreach ($hdr as $i => $c) { $s = self::text($c); if ($i === 0 || $s === '') { continue; } $regions[] = [$i, (str_contains($s, '剩余') || str_contains($s, '其余') || str_contains($s, '其他')) ? null : self::provincesIn($c)]; } } else { $regions = [[1, null]]; } foreach ($rowsBc as $r) { $bc = mb_strtoupper(trim(self::text($r[0]))); $table[$bc] = array_map(static fn ($reg) => [$reg[1], self::num($r[$reg[0]] ?? null)], $regions); } return [$table, $nogo, 'rows']; } foreach ($vals as $hi => $r) { $groups = []; foreach ((array) $r as $i => $c) { $s = self::text($c); if ($s !== '' && preg_match_all(self::BARCODE_RE, $s, $mm)) { $groups[] = [$i, array_map('mb_strtoupper', $mm[0])]; } } if ($groups === []) { continue; } $provRows = []; foreach (array_slice($vals, $hi + 1) as $rr) { if (is_array($rr) && ($rr[0] ?? null)) { $k = self::province($rr[0]); if (in_array($k, self::PROVINCES, true)) { $provRows[$k] = $rr; } } } foreach ($groups as [$i, $bcs]) { foreach ($bcs as $bc) { $list = []; foreach ($provRows as $p => $rr) { $list[] = [[$p => true], self::num($rr[$i] ?? null)]; } $table[$bc] = $list; } } return [$table, $nogo, 'cols']; } return [[], $nogo, 'none']; } private static function rawText(mixed $cell): string { return is_float($cell) ? self::text($cell) : (string) $cell; } private static function anyTruthy(array $cells): bool { foreach ($cells as $c) { if ($c !== null && $c !== '' && $c !== 0 && $c !== 0.0 && $c !== false) { return true; } } return false; } }