su_csv_to_html

The shortcodes-ultimate su csv to html function.

Published Date - February 15, 2021

Description

Create a HTML table from a CSV string.

su_csv_to_html( $csv, $delimiter, $header );

Parameters

  1. $csv

  2. $delimiter

  3. $header


Usage

The following example is for adding a hook callback.

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

// The input(s). 
$csv = null;
$delimiter = ',';
$header = false;

// NOTICE! Understand what this does before running.
$result = su_csv_to_html( $csv, $delimiter, $header );
            

Defined

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

shortcodes-ultimate/includes/functions-html.php

function su_csv_to_html( $csv, $delimiter = ',', $header = false ) {

	if ( ! is_string( $csv ) ) {
		return '';
	}

	if ( ! function_exists( 'str_getcsv' ) ) {
		return $csv;
	}

	$html = '';
	$rows = explode( PHP_EOL, $csv );

	foreach ( $rows as $row ) {

		$html .= '<tr>';

		foreach ( str_getcsv( $row, $delimiter ) as $cell ) {

			$cell = trim( $cell );

			$html .= $header
				? '<th>' . $cell . '</th>'
				: '<td>' . $cell . '</td>';

		}

		$html .= '</tr>';

		$header = false;

	}

	return '<table>' . $html . '</table>';

}