su_parse_range

The shortcodes-ultimate su parse range function.

Published Date - February 15, 2021

Description

Range converter.

su_parse_range( $string );

Parameters

  1. $string


Usage

The following example is for adding a hook callback.

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

// The input(s). 
$string = '';

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

Defined

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

shortcodes-ultimate/includes/functions-helpers.php

function su_parse_range( $string = '' ) {

	$parsed = array();

	foreach ( explode( ',', $string ) as $range ) {

		if ( strpos( $range, '-' ) === false ) {
			$parsed[] = intval( $range );
			continue;
		}

		$range = explode( '-', $range );

		if ( ! is_numeric( $range[0] ) ) {
			$range[0] = 0;
		}

		if ( ! is_numeric( $range[1] ) ) {
			$range[1] = 0;
		}

		foreach ( range( $range[0], $range[1] ) as $value ) {
			$parsed[] = $value;
		}

	}

	sort( $parsed );
	$parsed = array_unique( $parsed );

	return $parsed;

}