# mail-boxes-etc/trunk/lib/Helper/Csv.php

MBE eShip, version trunk. 88 lines.

- Page: https://pluginprobe.com/plugins/mail-boxes-etc/trunk/code/lib/Helper/Csv.php
- Raw: https://pluginprobe.com/plugins/mail-boxes-etc/trunk/raw/lib/Helper/Csv.php
- Modified: 2025-07-21T16:53:54+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/mail-boxes-etc/trunk/code/lib/Helper/Csv.php#L10-L20`.

```php
<?php

use MbeExceptions\ValidationException;

class Mbe_Shipping_Helper_Csv
{
    public function getCsvHeaderKnowDefinitions()
    {
        return $this->_csvHeaderKnowDefinitions;
    }

	/**
	 * @throws ValidationException
	 */
	public function readFile($importFilePath, $headerDefinition = [])
	{
		$file = fopen($importFilePath, "r");
		$i = 0;
		$header = null;
		$result = [];
		while (!feof($file)) {
			$currentRow = fgetcsv($file, 0, ",", '"');

			if (!empty($currentRow) && ([null]!==$currentRow)) {
				if ($i == 0) {
					//File heading
					$header = $currentRow;
				} else {
					try {
						$current  = $this->readCsvRowToArray( $headerDefinition, $header, $currentRow );
						$result[] = $current;
					} catch (ValidationException $e) {
						throw new ValidationException(esc_html($e->getMessage() . ' file:'. basename($importFilePath)  ));
					}
				}
			}
			$i++;
		}
		return $result;
	}

	/**
	 * @throws ValidationException
	 */
	function readCsvRowToArray($headerDefinition, $header, $row)
	{
		$result = [];
		if (is_array($row) && (count($row) === count($headerDefinition))) {
			foreach ($row as $index => $currentRowValue) {
				$headerName = $this->cleanString($header[$index]);
				$currentKey = $headerName;
				if (isset($headerDefinition[$headerName])) {
					$currentKey = $headerDefinition[$headerName];
				}
				$currentRowValue = trim($currentRowValue);
				/*
				$currentRowValue = str_replace("  ", " ", $currentRowValue);
				$currentRowValue = str_replace("  ", " ", $currentRowValue);
				$currentRowValue = str_replace("  ", " ", $currentRowValue);
				$currentRowValue = str_replace("„",createShipment "\"", $currentRowValue);
				$currentRowValue = str_replace("“", "\"", $currentRowValue);
				$currentRowValue = str_replace("”", "\"", $currentRowValue);
				*/
				$result[$currentKey] = $currentRowValue;
			}
		} else {
			throw new ValidationException(esc_html__('Expected ('. count($headerDefinition) .') columns, found ('. count($row) .').', 'mail-boxes-etc'));
		}
		return $result;
	}

    function cleanString($str)
    {
        $result = $str;
        $result = trim($result);
        $result = strtolower($result);
        $result = str_replace(" ", "_", $result);
        $result = str_replace(chr(160), "_", $result);
        $result = str_replace(chr(194), "_", $result);
        $result = str_replace(chr(195), "_", $result);
        $result = str_replace(",", "_", $result);
        $result = str_replace("/", "", $result);
        $result = str_replace("(", "", $result);
        $result = str_replace(")", "", $result);
        $result = str_replace("__", "_", $result);
        return $result;
    }
}
```
