If there is no consistent formatting and you need to match random color strings, you'll want to have a list of all possible colors. That might be hard if you're calling "melange" a color, which I don't think it is from googling that word. Anyway come up with the colors you can expect, and check if they exist in the string.
Version with color matching in regex.
<?php
$colors = ['Red', 'Orange', 'Yellow', 'Black', 'Blue', 'Grey'];
$colorStr = implode('|', $colors);
$colorPattern = "/({$colorStr})/i";
$sizePattern = '/Size: (\w+)/i';
$rows = [
'Retractor Color: Blue',
'Robin Jacket Color: Black (Body) & Red (Stripe)Size: L',
'Ladies Clutch in Green',
'T-Shirt Color: BlackSize: L',
'Full Sleeve Sweat Shirt Color: Grey Melange (Light Grey)Size: S',
'T-Shirt Color: BlackSize: L',
'Speed Jacket Size: M'
];
$output = [];
foreach($rows AS $row){
$data = ['product'=>$row];
preg_match($colorPattern, $row, $colorsFound);
if(isset($colorsFound[1])){
array_shift($colorsFound);
$data['colors'] = $colorsFound;
}
preg_match($sizePattern, $row, $sizes);
if(isset($sizes[1])){
$data['size'] = $sizes[1];
}
$output[] = $data;
}
var_dump($output);
https://eval.in/402722
(my original version is below, the regex one is better tho.)
<?php
$colors = ['Red', 'Orange', 'Yellow', 'Black', 'Blue', 'Grey'];
$rows = [
'Retractor Color: Blue',
'Robin Jacket Color: Black (Body) & Red (Stripe)Size: L',
'Ladies Clutch in Green',
'T-Shirt Color: BlackSize: L',
'Full Sleeve Sweat Shirt Color: Grey Melange (Light Grey)Size: S',
'T-Shirt Color: BlackSize: L',
'Speed Jacket Size: M'
];
$output = [];
foreach($rows AS $row){
$data = ['product'=>$row];
foreach($colors AS $color){
if(stripos($row, $color) !== false){
$data['colors'][] = $color;
}
}
preg_match('/Size: (\w+)/', $row, $matches);
if(isset($matches[1])){
$data['size'] = $matches[1];
}
$output[] = $data;
}
https://eval.in/402712
color:andsize:were consistent but what is theClutch in Green?preg_match().