我正在将1000辆车辆导入我的Woocommerce网站,这些网站是通过来自多个不同经销商网站的XML Feed导入的。
每个经销商都使用自己的“颜色”命名约定,因此我们最终会得到大量不必要的颜色变化。在我将他们的Feed处理到我们的网站之前,我正在运行下面的功能,将他们的颜色转换为我们的颜色。
在我下面的示例代码中,您可以看到我有几个“蓝色”变体,如蓝莓,蓝火,蓝金属等。
我一直在使用下面的代码并将经销商颜色转换为我的颜色,但每次经销商添加新颜色或输入拼写错误时,我都无法继续更新列表。
如何修改此代码,使其不检查完全匹配,而是查找经销商颜色中的根词(蓝色),如果在任何地方找到带蓝色的字符串,则会自动为其指定新的代码只是我的颜色“蓝色”。
<?php
function my_color($value) {
$color = array(
'60Th Anniversary Yellow' => 'Yellow',
'Alpine White' => 'White',
'Aluminum' => 'Silver',
'Aqua' => 'Blue',
'Bk' => 'Black',
'Black' => 'Black',
'Black Cherry' => 'Black',
'Black Denim' => 'Black',
'Black Metallic' => 'Black',
'Black Pearl' => 'Black',
'Black Pearl Metallic' => 'Black',
'Black Satin' => 'Black',
'Black-Deluxe' => 'Black',
'Blaze Orange' => 'Orange',
'Blazed Green' => 'Green',
'Blck' => 'Black',
'Blk' => 'Black',
'Bluberry' => 'Blue',
'Blue' => 'Blue',
'Blue Fire' => 'Blue',
'Blue Metallic' => 'Blue',
'Blue White' => 'White',
'Blueberry' => 'Blue',
'Blueberyy' => 'Blue',
'Bright White' => 'White',
'Bronze' => 'Tan',
'Brown' => 'Tan',
'Brushed' => 'Silver',
'Brushed Aluminum' => 'Silver',
'Burgandy' => 'Red',
'Burgundy' => 'Red',
'Cadmium Yellow' => 'Yellow',
'Camo' => 'Camo',
'Camo Gray' => 'Camo',
'Camo Gre' => 'Camo',
'Camo Green' => 'Green',
'Camogreen' => 'Green',
'Camp Green' => 'Green',
'Candy Burnt Orange' => 'Orange',
'Candy Cascade Blue' => 'Blue',
'Candy Lime Green' => 'Green',
'Candy Persimmon Red' => 'Red',
'Candy Red' => 'Red',
'Carbon' => 'Grey',
'Carbon Gray' => 'Grey',
'Carbon Metallic' => 'Silver',
'Charcoal Silver' => 'Silver',
'Cherry' => 'Red',
'Chili Pepper' => 'Red',
'Chrome' => 'Silver',
'Cognac' => 'Tan',
'Comp Silver' => 'Silver',
'Competition Silver' => 'Silver',
'Cool Blue Pearl' => 'Blue',
'Cool Candy Red' => 'Red',
'Copper' => 'Tan',
'Copper Metallic' => 'Tan',
'Crimson' => 'Red',
'Crimson Metallic' => 'Red',
'Custom' => '',
'Dark Grey' => 'Grey',
'Dark Metallic Green' => 'Green',
'Dark Red Metallic' => 'Red',
'Dark Royal Red' => 'Red',
'Daytona Blue' => 'Blue',
'Deep Blue' => 'Blue',
'Ebony' => 'Black',
'Ember Red Sunglow' => 'Red',
'Flat Black' => 'Black',
'Flat Ebony' => 'Black',
'Galv' => '',
'Gldn Grn' => 'Green',
'Gold' => 'Gold',
'Granite Gray' => 'Grey',
'Graphite' => 'Grey',
'GRAPHITE CRYSTAL' => 'Grey',
'GRAY' => 'Gray',
'GRAY BLUE METALLIC' => 'Blue',
'GRAYSTONE' => 'Grey',
'GREEN' => 'Green',
'GREY' => 'Grey',
'HAV RED' => 'Red',
'HAVASU PEARL RED' => 'Red',
'HONDA PHANTOM CAMO' => 'Camo',
'HUNTER GREEN' => 'Green',
'IMPACT BLUE' => 'Blue',
'INDY RED' => 'Red',
'INTENSE RED' => 'Red',
'INTENSE RED PEARL' => 'Red',
'KEY LIME' => 'Green',
'LAVA RED' => 'Red',
'LIGHT GRE' => 'Green',
'LIGHT GREY' => 'Grey',
'LIGHT SILVER METALLIC' => 'Silver',
'LIGUID GRAPHITE' => 'Grey',
'LIME' => 'Green',
'LIME GREEN' => 'Green',
'LIME GREEN ' => 'Green',
'LIME GRN' => 'Green',
'LIQUID GRAPHITE' => 'Grey',
'LIQUID SILVER' => 'Silver',
'MAGNESIUM' => 'Silver',
'MAGNESIUM METALLIC' => 'Silver',
'MALDIVES BLUE' => 'Blue',
'MALDIVES GREE' => 'Green',
'MAROON' => 'Red',
'MATTE BLACK' => 'Black',
'MATTE BLACK METALLIC' => 'Black',
'MATTE GRAY' => 'Grey',
'MATTE GREY' => 'Grey',
'MATTE SILVER' => 'Silver',
'MATTE TITAN' => 'Silver',
'MATTE WHITE LIGHTNING' => 'White',
'MERLO' => 'Silver',
'METALLIC' => 'Silver',
'METALLIC BLACK' => 'Black',
'METALLIC BLUE' => 'Blue',
'METALLIC FLAT RAW GRAYSTO' => 'Silver',
'METALLIC GRAYSTONE' => 'Silver',
'METALLIC IMPERIAL BLUE' => 'Blue',
'METALLIC MATTE CARBON GRA' => 'Silver',
'METALLIC PHANTOM SILVER' => 'Silver',
'METALLIC SILVER' => 'Silver',
'METALLIC SPARK BLACK' => 'Black',
'MOSSY OA' => 'Camo',
'MOSSY OAK' => 'Camo',
'MOSSY OAK BREAK-UP' => 'Camo',
'MOSSY OAK CAMO' => 'Camo',
'MULTI' => '',
'NUCLEAR SUNSET' => 'Orange',
'null' => '',
'OLIVE' => 'Green',
'ORANGE' => 'Orange',
'ORANGE BURST' => 'Orange',
'ORANGE-MATT' => 'Orange',
'ORBITAL BLUE' => 'Blue',
'P.P. CAMO' => 'Camo',
'PASSION RED' => 'Red',
'PEARL BLUE' => 'Blue',
'PEARL CRYSTAL WHITE' => 'White',
'PEARL RED' => 'Red',
'PEARL STARDUST WHITE' => 'White',
'PEARL WHI' => 'White',
'PEARL WHITE' => 'White',
'PEARL WHT' => 'White',
'PERSIMMON RED' => 'Red',
'PEWTER' => 'Grey',
'PHANTOM' => 'Camo',
'PHANTOM BLACK' => 'Black',
'PHANTOM CAMO' => 'Camo',
'PINEAPPLE' => 'Yellow',
'PINK' => 'Red',
'POLARIS P' => '',
'POLARIS PURSUIT CAMO' => 'Camo',
'PURPLE' => 'Purple',
'PURSUIT CAMO' => 'Camo',
'RACING BLUE' => 'Blue',
'RADICAL WHITE' => 'White',
'Rapid Red' => 'Red',
#N/A
'REALTREE' => 'Camo',
'REALTREE AP HD W' => 'Camo',
'REALTREE APG HD' => 'Camo',
'REALTREE EXTRA' => 'Camo',
'REALTREE EXTRA CAMO' => 'Camo',
'REALTREE XTRA' => 'Camo',
'REALTREE XTRA CAMO' => 'Camo',
'REALTREE XTRA GREEN CAMO' => 'Green',
'RED' => 'Red',
'RED GREEN' => 'Red',
'RED HOT SUNGLOW' => 'Red',
'RED KHAKI' => 'Red',
'RED PEARL' => 'Red',
'RED USED TO BE WHITE' => 'Red',
'RED W' => 'Red',
'REDDISH COPPER' => 'Red',
#N/A
'ROOT BEER' => 'Tan',
'ROSEWOOD BROWN' => 'Tan',
'SAGE GRE' => 'Green',
'SAGE GREEN' => 'Green',
'SALVAGE' => '',
'SCARLET RED' => 'Red',
'SE TRI-COLOR' => '',
'SILVER' => 'Silver',
'SILVER DENIM' => 'Blue',
'SILVER METALLIC' => 'Silver',
'SILVER SATIN' => 'Silver',
'SILVER-BLUE' => 'Blue',
'SL' => '',
'SOLAR RED' => 'Red',
'SPARK BLACK' => 'Black',
'SPARTAN RED' => 'Red',
'SPECTRA O' => '',
'SPECTRA ORANGE' => 'Orange',
'STARDUST SILVER' => 'Silver',
'STARDUST WHITE' => 'White',
'STEALTH BLACK' => 'Black',
'STEEL BLUE' => 'Blue',
'STEEL BLUE W' => 'Blue',
'STEEL GREY' => 'Grey',
'STONE GRAY' => 'Grey',
'SUNBEAM RED' => 'Red',
'SUNSET RE' => 'Red',
'SUNSET RED' => 'Red',
'SUPER BLA' => '',
'SUPER BLACK' => 'Black',
'SUPER GRAPHITE' => 'Grey',
'TAN' => 'Tan',
'TEAL GREEN' => 'Green',
'TEAM YAMAHA BLUE' => 'Blue',
'TIMBERLINE' => 'Grey',
'TIMBERLINE GREEN' => 'Green',
'TITANIUM' => 'Silver',
'TITANIUM M' => 'Silver',
'TITANIUM MATTE METAL' => 'Silver',
'TITANIUM MATTE METALLIC' => 'Silver',
'TITANIUM METALLIC' => 'Silver',
'TRAIL BLACK PEARL' => 'Black',
'TRUE TIMBER XD3 CAMO' => 'Camo',
'TURBO SIL' => 'Silver',
'TURBO SILVER' => 'Silver',
'ULTRA BLUE METALLIC' => 'Blue',
'VANILLA' => 'White',
'VANILLA WHITE' => 'White',
'VELOCITY BLUE' => 'Blue',
'VIPER RED' => 'Red',
'VOODOO B' => '',
'VOODOO BLUE' => 'Blue',
'WHI' => 'White',
'WHITE' => 'White',
'WHITE ' => 'White',
'WHITE BLUE' => 'White',
'WHITE GREEN' => 'Green',
'WHITE LIGH' => 'White',
'WHITE LIGHTNING' => 'White',
'WHITE PEARL' => 'White',
'WHITE-DELUXE' => 'White',
'WHT' => 'White',
'YACHT BLUE' => 'Blue',
'YAMAHA BLUE' => 'Blue',
'YAMAHA YELLOW' => 'Yellow',
'YELLOW' => 'Yellow',
'YELLOW ' => 'Yellow',
'YELLOW METAL' => 'Yellow',
'YELLOW SILVER BLACK' => 'Yellow',
);
if (isset($color[$value])) return $color[$value];
return $value;
}
?>
答案 0 :(得分:0)
我在这里写了一个函数。虽然你可以使用它,但它没有完全的帮助。
$a = checkColor('LIGHT SILVER METALLIC');
function checkColor($value){
//You can add more colors here
$colors = ['Blue', 'White', 'Orange', 'Green', 'Black', 'Silver'];
foreach ($colors as $color) {
if (stripos($value, $color) !== false) {
$result = $color;
break;
}
}
if(!empty($result))
return $result;
}
因此,当您在上面的函数中传递汽车颜色时,它会检查子字符串,如果存在,则相应地返回。 但是,对于铝,水,拉丝等颜色我看不到任何方式来分配你自己的颜色..
答案 1 :(得分:0)
如果有人对此感兴趣,那么我必须开始工作的最后一项功能。它是不区分大小写的,它将检查根词是否作为部分匹配存在。
<?php
function my_condition($value) {
$condition = array(
'Cons' => 'Used',
);
if (isset($condition[$value])) return $condition[$value];
return $value;
};
function my_color( $original ) {
$colors = array(
'yellow' => 'Yellow',
'white' => 'White',
'silver' => 'Silver',
'blue' => 'Blue',
'black' => 'Black',
'orange' => 'Orange',
'green' => 'Green',
'tan' => 'Tan',
'red' => 'Red',
'camo' => 'Camo',
'grey' => 'Grey',
'gold' => 'Gold',
'gray' => 'Gray',
'grey' => 'Gray',
'purple' => 'Purple',
'maroon' => 'Red',
'brown' => 'Tan',
'aqua' => 'Blue',
'blk' => 'Black',
'slv' => 'Silver',
'bronze' => 'Tan',
'copper' => 'Tan',
'crimson' => 'Red',
'chili' => 'Red',
'ebony' => 'Black',
'lime' => 'Green',
'mossy' => 'Camo',
'pewter' => 'Grey',
'graphite' => 'Grey',
'vanilla' => 'White',
'titanium' => 'Silver',
'sage g' => 'Green',
'aluminum' => 'Silver',
'black/' => 'Black',
'raven' => 'Black',
'repsol' => 'Orange',
'burgandy' => 'Red',
'cherry' => 'Red',
'carbon' => 'Black',
'olive' => 'Green',
);
$return = 'unknown';
foreach($colors as $matching_color => $common_color)
if(strpos(strtolower($original) , strtolower($matching_color)) !== false)
$return = $common_color;
return $return;
}
?>