如何获取要排除的号码超过 13 个的 EAN (PHP)



我为一个网站制作了这个脚本,它基本上与供应商同步。现在我的问题是供应商开始为每个产品提供多个 EAN。我需要让脚本检查产品是否超过 1 个 ean (一个 ean 包含 13 个数字(,如果它有超过 13 个数字,则只采用 ean 的前 13 位数字。我找不到任何解决方案

update_post_meta( $id, '_sku', $ean);
update_post_meta( $id, 'productcode', $bfc);
update_post_meta( $id, '_stock', $qty);
update_post_meta( $id, '_price', $selling_price);
update_post_meta( $id, '_regular_price', $selling_price);
wp_untrash_post($id);
#update_post_meta($id, 'channable_sync', 1);
if ($qty > 0) {
update_post_meta($id, '_stock_status', 'instock');
} else {
update_post_meta($id, '_stock_status', 'outofstock');
}
if ($ean ==> 13) {
}

为了检查 EAN 是否为>=13 个字符,您使用strlen(),对于前 13 个字符,您使用preg_match()和正则表达式 ((d{13})(。

<?php
$ean = "123456789012345";
if(strlen($ean) >= 13) {
echo "yes more than 13 charsn";
}
preg_match('/(d{13})/', $ean, $matches);
echo $matches[0];

为避免对 db 进行双重写入,请首先检查 $ean 是否超过 13 位(字符(,如果是,则将其截断为 13 位,然后将其存储在 db 中

if(strlen($ean) > 13){
//is longer than 13
$ean = substr($ean, 0, 13); //truncate to 13 chars
}
update_post_meta( $id, '_sku', $ean);
update_post_meta( $id, 'productcode', $bfc);
//the rest of your code

或单行代替:$ean = strlen($ean) > 13 ? substr($ean, 0, 13) : $ean;

我假设您的$ean是等于或小于PHP_INT_MAX的字符串或整数类型

最新更新