0

I have multiple rows in database like below

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

I need to extract color and size information for every row into a separate column to identify easily. Since I am not good at regex, can someone please help me how to achieve this.

EDIT:

I need output as below enter image description here

4
  • It seemed like color: and size: were consistent but what is the Clutch in Green? Commented Jul 22, 2015 at 1:52
  • Green is the color, the string will not have color: consistently Commented Jul 22, 2015 at 1:59
  • Maybe you should use preg_match(). Commented Jul 22, 2015 at 2:26
  • @ PHPglue, I have no idea about using preg_match, can you please help Commented Jul 22, 2015 at 2:31

2 Answers 2

2

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) &amp; 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) &amp; 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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot, this really helps.
"That might be hard if you're calling "melange" a color, which I don't think it is from googling that word." - "Melange" is french for "mix" ;-) However, the proper spelling is "mélange" with an "é".
For future iterations if the terms you are searching for will have special characters use preg_quote. Example $colorPattern = "/(" . preg_quote($colorStr) . ")/i"; php.net/manual/en/function.preg-quote.php.
@AnandaTheerthanJ : The solution provided in this answer is incomplete. My solution is complete and gives you EXACTLY what you want.
0

My solution uses a combination of explode, str_replace and preg_replace to do the parsing and has the following features :

  • You do not need to define an array with possible options for your colors or sizes. You can use ANY color string and ANY size value
  • All rows have three colums.
    • Empty columns have value NULL
  • You can use both Color: and in to define your color values
    • For all color values, any ( ) and its content is removed
    • For all color values, &amp; is replaced with ,

Demo :

https://eval.in/402731


Code :

<?php

$string = "Retractor Color: Blue
Robin Jacket Color: Black (Body) &amp; 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";

// Split string on new line character
$data = explode("\n", $string);
$output = [];
// Loop through lines
foreach ($data as $i => $value) {
    $value = trim($value);
    // Split string on "Color: "
    $data[$i] = explode("Color: ", $value);
    // If color value is not found
    if (count($data[$i]) < 2) {
        // Split string on " in "
        $data[$i] = explode(" in ", $data[$i][0]);
    }
    // If color value is still not found
    if (count($data[$i]) < 2) {
        // Split string on "Size: "
        $data[$i] = explode("Size: ", trim($data[$i][0]));
        //Build output
        $output[$i] = ['Product' => $value, 'Color' => null, 'Size' => $data[$i][1]];
    } else {
        // Split string on "Size: "
        $data[$i] = explode("Size: ", trim($data[$i][1]));
        //Remove " (__) " and replace " &amp ";
        $data[$i][0] = preg_replace('/[ ]?[\(].*[\)][ ]?/U', '', str_replace(' &amp; ', ',', $data[$i][0]));
        // If size value is not found
        if (count($data[$i]) < 2) {
            // Set NULL value for missing Size
            $data[$i][1] = NULL;
        }
        // Build output
        $output[$i] = ['Product' => $value, 'Color' => $data[$i][0], 'Size' => $data[$i][1]];
    }
}

var_dump($output);
?>

Output :

array(7) {
  [0]=>
  array(3) {
    ["Product"]=>
    string(21) "Retractor Color: Blue"
    ["Color"]=>
    string(4) "Blue"
    ["Size"]=>
    NULL
  }
  [1]=>
  array(3) {
    ["Product"]=>
    string(58) "Robin Jacket Color: Black (Body) &amp; Red (Stripe)Size: L"
    ["Color"]=>
    string(9) "Black,Red"
    ["Size"]=>
    string(1) "L"
  }
  [2]=>
  array(3) {
    ["Product"]=>
    string(22) "Ladies Clutch in Green"
    ["Color"]=>
    string(5) "Green"
    ["Size"]=>
    NULL
  }
  [3]=>
  array(3) {
    ["Product"]=>
    string(28) "T-Shirt  Color: BlackSize: L"
    ["Color"]=>
    string(5) "Black"
    ["Size"]=>
    string(1) "L"
  }
  [4]=>
  array(3) {
    ["Product"]=>
    string(63) "Full Sleeve Sweat Shirt Color: Grey Melange (Light Grey)Size: S"
    ["Color"]=>
    string(12) "Grey Melange"
    ["Size"]=>
    string(1) "S"
  }
  [5]=>
  array(3) {
    ["Product"]=>
    string(28) "T-Shirt  Color: BlackSize: L"
    ["Color"]=>
    string(5) "Black"
    ["Size"]=>
    string(1) "L"
  }
  [6]=>
  array(3) {
    ["Product"]=>
    string(21) "Speed Jacket  Size: M"
    ["Color"]=>
    NULL
    ["Size"]=>
    string(1) "M"
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.