su_parse_csv

The shortcodes-ultimate su parse csv function.

Published Date - February 15, 2021

Description

su_parse_csv( $file );

Parameters

  1. $file


Usage

The following example is for adding a hook callback.

if ( !function_exists( 'su_parse_csv' ) ) {
    require_once ABSPATH . PLUGINDIR . 'shortcodes-ultimateshortcodes-ultimate/includes/deprecated/functions.php';
}

// The input(s). 
$file = null;

// NOTICE! Understand what this does before running.
$result = su_parse_csv( $file );
            

Defined

The function is defined in the following location(s).

shortcodes-ultimate/includes/deprecated/functions.php

function su_parse_csv( $file ) {
	// phpcs:disable
	$csv_lines = file( $file );
	if ( is_array( $csv_lines ) ) {
		$cnt = count( $csv_lines );
		for ( $i = 0; $i < $cnt; $i++ ) {
			$line       = $csv_lines[ $i ];
			$line       = trim( $line );
			$first_char = true;
			$col_num    = 0;
			$length     = strlen( $line );
			for ( $b = 0; $b < $length; $b++ ) {
				if ( $skip_char != true ) {
					$process = true;
					if ( $first_char == true ) {
						if ( $line[ $b ] == '"' ) {
							$terminator = '";';
							$process    = false;
						} else {
							$terminator = ';';
						}
						$first_char = false;
					}
					if ( $line[ $b ] == '"' ) {
						$next_char = $line[ $b + 1 ];
						if ( $next_char == '"' ) {
							$skip_char = true;
						} elseif ( $next_char == ';' ) {
							if ( $terminator == '";' ) {
								$first_char = true;
								$process    = false;
								$skip_char  = true;
							}
						}
					}
					if ( $process == true ) {
						if ( $line[ $b ] == ';' ) {
							if ( $terminator == ';' ) {
								$first_char = true;
								$process    = false;
							}
						}
					}
					if ( $process == true ) {
						$column .= $line[ $b ];
					}
					if ( $b == ( $length - 1 ) ) {
						$first_char = true;
					}
					if ( $first_char == true ) {
						$values[ $i ][ $col_num ] = $column;
						$column                   = '';
						$col_num++;
					}
				} else {
					$skip_char = false;
				}
			}
		}
	}
	$return = '<table><tr>';
	foreach ( $values[0] as $value ) {
		$return .= '<th>' . $value . '</th>';
	}
	$return .= '</tr>';
	array_shift( $values );
	foreach ( $values as $rows ) {
		$return .= '<tr>';
		foreach ( $rows as $col ) {
			$return .= '<td>' . $col . '</td>';
		}
		$return .= '</tr>';
	}
	$return .= '</table>';
	return $return;
	// phpcs:enable
}