explode textarea and commas
Abstract class is ideal for this:

abstract class ProductsTextareaBase {

  protected abstract function getColumns();

  public function parse($productsTextarea) {
    $products = explode("\r\n", $productsTextarea); // sanitize

    $y = 0; 
    $productArray = array();

    // explode and trim each product, assign values to an array
    foreach($products as $product) {      
        $product = array_map('trim', explode(',', $product));
        $i = 0;
        $columns = $this-&gt;getColumns();
        foreach($product as $col) {
          $productArray[$y][$columns[$i]] = $col;
          $i++;
        }
        $y++;
    }
    return $productArray;
  }
}

class WidgetProductsTextarea extends ProductsTextareaBase {
  protected function getColumns() {
    return array('name', 'cost', 'quantity');
  }
}