Import CSV data

@param   string  $string
@return  array
481134
protected function _fromCsv($string)
	{
		$data = array();

		$rows = preg_split('/(?<='.preg_quote(Arr::get($this->config, 'csv.enclosure', '"')).')'.Arr::get($this->config, 'csv.regex_newline', '\n').'/', trim($string));

		// csv config
		$delimiter = Arr::get($this->config, 'csv.delimiter', ',');
		$enclosure = Arr::get($this->config, 'csv.enclosure', '"');
		$escape = Arr::get($this->config, 'csv.escape', '\\');

		// Get the headings
		$headings = str_replace($escape.$enclosure, $enclosure, str_getcsv(array_shift($rows), $delimiter, $enclosure, $escape));

		foreach ($rows as $row)
		{
			$data_fields = str_replace($escape.$enclosure, $enclosure, str_getcsv($row, $delimiter, $enclosure, $escape));

			if (count($data_fields) == count($headings))
			{
				$data[] = array_combine($headings, $data_fields);
			}

		}

		return $data;
	}
607860
protected function _from_csv($string, $no_headings = false)
	{
		$data = array();

		// csv config
		$newline = \Config::get('format.csv.regex_newline', "\n");
		$delimiter = \Config::get('format.csv.delimiter', \Config::get('format.csv.import.delimiter', ','));
		$escape = \Config::get('format.csv.escape', \Config::get('format.csv.import.escape', '"'));
		// have to do this in two steps, empty string is a valid value for enclosure!
		$enclosure = \Config::get('format.csv.enclosure', \Config::get('format.csv.import.enclosure', null));
		$enclosure === null and $enclosure = '"';

		if (empty($enclosure))
		{
			$rows = preg_split('/(['.$newline.'])/m', trim($string), -1, PREG_SPLIT_NO_EMPTY);
		}
		else
		{
			$rows = preg_split('/(?<=[0-9'.preg_quote($enclosure).'])'.$newline.'/', trim($string));
		}

		// Get the headings
		if ($no_headings !== false)
		{
			$headings = str_replace($escape.$enclosure, $enclosure, str_getcsv(array_shift($rows), $delimiter, $enclosure, $escape));
			$headcount = count($headings);
		}

		// Process the rows
		$incomplete = '';
		foreach ($rows as $row)
		{
			// process the row
			$data_fields = str_replace($escape.$enclosure, $enclosure, str_getcsv($incomplete.($incomplete?$newline:'').$row, $delimiter, $enclosure, $escape));

			// if we didn't have headers, the first row determines the number of fields
			if ( ! isset($headcount))
			{
				$headcount = count($data_fields);
			}

			// finish the row if the have the correct field count, otherwise add the data to the next row
			if (count($data_fields) == $headcount)
			{
				$data[] = $no_headings === false ? $data_fields : array_combine($headings, $data_fields);
				$incomplete = '';
			}
			else
			{
				$incomplete = $row;
			}
		}

		return $data;
	}
245367
public function parse($string, array $vars = [])
    {
        $output = $string;

        if(is_file($string)) {
            $output = file_get_contents($string);
        }

        if($this->count()) {
            foreach($this->registry as $adapter) {
                if($adapter instanceof Abstracts\AbstractAdapter) {
                    if($adapter->isSupported()) {
                        $adapter->initialize();
                        $adapter->loadString($output);
                        $output = $adapter->parse($vars);
                    }
                }
            }
        }

        return $output;
    }
457253
public static function csplit($string, $delimiter = ',')
    {
        if (is_array($string)) {
            return $string;
        }
        $res = [];
        foreach (explode($delimiter, $string) as $k => $v) {
            $v = trim($v);
            if (strlen($v)) {
                array_push($res, $v);
            }
        }

        return $res;
    }
323889
protected function mapFields($data)
    {
        $result = [];
        $index = 0;

        foreach ($this->csvFileFieldsOrder as $value) {
            if (!empty($value)) {
                if (strtolower($data[$index]) == 'null') {
                    $result[$value] = null;
                } else {
                    $result[$value] = $data[$index];
                }
            }
            $index++;
        }

        return $result;
    }
596847
public static function stringToArray(?string $string): array {
        $result = [];
        $strLen = \strlen($string);
        if (null === $string) return $result;
        if (1 === $strLen) {
            $result[] = $string;
            return $result;
        }
        for ($i = 0; $i < $strLen; $i++) {
            $result[] = $string[$i];
        }
        return $result;
    }
593071
public static function stringToArray(string $string): array
    {
        list($from, $to) = explode(self::SEPARATOR, $string);
        $from = empty($from)
            ? self::ZERO
            : (is_numeric($from)
                ? (int) $from
                : $from);

        $to = empty($to)
            ? self::INFINITE
            : (is_numeric($to)
                ? (int) $to
                : $to);

        return [$from, $to];
    }
486962
public static function jsonToArray(string $string): array
    {
        static $jsonErrors = [
            JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded',
            JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch',
            JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found',
            JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON',
            JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded',
        ];

        $data = json_decode($string, true);

        if (JSON_ERROR_NONE !== json_last_error()) {
            $last = json_last_error();

            throw new \RuntimeException(
                'Unable to parse JSON data: '
                .(isset($jsonErrors[$last]) ? $jsonErrors[$last] : 'Unknown error')
            );
        }

        return $data;
    }
1371793
public static function ordStr($string)
    {
        $codes = array();

        $encoding = mb_detect_encoding($string);
        $size = mb_strlen($string, $encoding);

        for ($i = 0; $i < $size; $i++) {
            $char = mb_substr($string, $i, 1, $encoding);
            $codes[] = static::ord($char);
        }

        return $codes;
    }
707172
protected static function _parse($string) {
		if (substr_count($string, '::') !== 2) {
			throw new Exception('Possible data tampering: form signature string has wrong format.');
		}
		list($locked, $excluded) = explode('::', $string, 3);

		return [
			'locked' => unserialize(urldecode($locked)),
			'excluded' => unserialize(urldecode($excluded))
		];
	}
1046877
public function csvToArray($value)
    {
        if ($value === null) {
            return [];
        }

        $result = explode(',', $value);
        $result = array_map('trim', $result);
        $result = array_filter($result);

        return array_values($result);
    }
34041
private function import(&$data)
    {
        $imports = array();
        preg_match_all('/#@([-a-z0-9\/_]+)\\n/is', $data, $imports, PREG_SET_ORDER);
        foreach ($imports as $import) {
            $importPath = FS2LANG . '/' . $this->local . '/' . $import[1] . '.txt';
            $importData = @file_get_contents($importPath);

            // getting file content ok
            if ($importData != false) {
                $importData = str_replace(array("\r\n", "\r"), "\n", $importData); // unify linebreaks
                $this->import($importData);
                $replace = '/#@' . preg_quote($import[1], '/') . '/i';
                $data = preg_replace($replace, $importData, $data);
                // replace all imports recursive
            }
        }
        unset($imports);
    }
739742
private function splitString($string)
    {
        $result = array();
        for ($i = 0; $i < mb_strlen($string, "UTF-8"); ++$i) {
            $result[] = mb_substr($string, $i, 1, "UTF-8");
        }
        return $result;
    }
1238410
public static function stringToValueAndUnit(string $string): array
    {
        preg_match('/^([0-9.]+)([a-zA-Z]+)$/', $string, $matches);
        $value = (float) $matches[1];
        $unit = Mapper::getUnitFromAbbreviation($matches[2]);
        return [$value, $unit];
    }
1232534
public static function csvToArray($csv)
    {
        // currently need to write out to file to use parser
        $tmpDir = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
        $filename = $tmpDir . 'csv_import' . time() . '.csv';
        file_put_contents($filename, $csv);

        // assume first row is field header
        $result = [];
        ini_set('auto_detect_line_endings', true);
        if (($handle = fopen($filename, "r")) !== false) {
            $headers = fgetcsv($handle, null, ",");
            while (false !== ($row = fgetcsv($handle))) {
                $new = [];
                foreach ($headers as $key => $value) {
                    $new[$value] = array_get($row, $key);
                }

                $result[] = $new;
            }

            fclose($handle);
            unlink($filename);
        }

        return $result;
    }
798793
public function importFromCsv($file)
    {
        $data = file_get_contents($file);
        $data = $this->container->get('claroline.utilities.misc')->formatCsvOutput($data);
        $lines = str_getcsv($data, PHP_EOL);
        $this->om->startFlushSuite();
        $i = 0;

        foreach ($lines as $line) {
            $values = str_getcsv($line, ';');
            $code = $values[0];
            $workspace = $this->om->getRepository('ClarolineCoreBundle:Workspace\Workspace')->findOneByCode($code);

            $name = $values[1];
            $tab = $this->om->getRepository('ClarolineCoreBundle:Tab\HomeTab')->findBy(['workspace' => $workspace, 'name' => $name]);
            if (!$tab) {
                $this->createHomeTab($name, $workspace);
                ++$i;
            } else {
                $this->log("Tab {$name} already exists for workspace {$code}");
            }

            if (0 === $i % 100) {
                $this->om->forceFlush();
                $this->om->clear();
            }
        }

        $this->om->endFlushSuite();
    }
274813
public function importCsv($row)
	{
		
		$row = str_getcsv($row);
		
		$this->prefectureCode = (int)$row[0];
		$this->prefectureName = (string)$row[1];
		$this->municipalityCode = (int)$row[2];
		$this->municipalityName = (string)$row[3];
		$this->localCode = (int)$row[4];
		$this->localName = (string)$row[5];
		$this->lat = (float)$row[6];
		$this->lng = (float)$row[7];
		
		return $this;
	}