<?php
function getColors($item_colors, $result = []){
preg_match_all('/(\w+),|(\w+)\/(\w+)/', $item_colors, $colors);
foreach ($colors as $key => $value) {
if ($key === 0) continue;
array_push($result, ...$value);
}
return array_filter($result);
}
$item_colors = "Vario Base Unit with steel, large, black/orange";
echo implode(' ', getColors($item_colors));
//steel large black orange
var_dump(getColors($item_colors));
// array(4) {
// [0]=>
// string(5) "steel"
// [1]=>
// string(5) "large"
// [5]=>
// string(5) "black"
// [8]=>
// string(6) "orange"
// }
Live:
https://3v4l.org/JqTN1
Ещё вариант:
<?php
$item_colors = "Vario Base Unit with steel, large, black/orange";
$result = [];
foreach (explode(', ', str_replace('Vario Base Unit with ','', $item_colors)) as $color) {
$colors = explode('/', $color);
count($colors) === 2 ? array_push($result, ...$colors) : array_push($result, $color);
}
echo implode(' ', $result);
//steel large black orange
var_dump($result);
// array(4) {
// [0]=>
// string(5) "steel"
// [1]=>
// string(5) "large"
// [2]=>
// string(5) "black"
// [3]=>
// string(6) "orange"
// }
Live:
https://3v4l.org/1JR98
Ещё вариант для извращенцев)
<?php
$item_colors = "Vario Base Unit with steel, large, black/orange";
$result = eval('return '.str_replace(['Vario Base Unit with ', '/', ', '], ['["',', ', '", "'], $item_colors).'"];');
echo implode(' ', $result);
//steel large black orange
var_dump($result);
// array(4) {
// [0]=>
// string(5) "steel"
// [1]=>
// string(5) "large"
// [2]=>
// string(5) "black"
// [3]=>
// string(6) "orange"
// }
Live:
https://3v4l.org/TBdId