note 86037 added to function.range

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
Range as a string. Items are separated with a comma; which can be in any of the following formats:

"1, 2, 3, 4, 5, 6" - output: 1, 2, 3, 4, 5, 6
"1 - 6"  - output: 1, 2, 3, 4, 5, 6
"1 -%2 6" - output: 1, 3, 5 (last number will not be counted unless it evenly fits in)
"1 - -6" - output: 1, 0, -1, -2, -3, -4, -5, -6
"0 - 0" - output: 0
"1, 2, 3, [LAST_NUM] - 6" - output: 1, 2, 3, 3, 4, 5, 6 (note repeated 3)
"1, 2, 3, [LAST_NUM+1] - 6" - output: 1, 2, 3, 4, 5, 6 (no repeated 3)
"1, 2, 3, [LAST_NUM+-1] - 6" - output: 1, 2, 3, 2, 3, 4, 5, 6

<?php

define('RANGE_ARRAY', 1);
define('RANGE_STRING', 2);

function range_string($range_str, $output_type = RANGE_ARRAY)
{
	$range_out = array();
	$ranges = explode(",", $range_str);
	
	$last_num = 0;
	
	foreach($ranges as $range)
	{
		$step = 1;
		$range = trim($range);
		
		if(is_numeric($range))
		{
			// Just a number; add it to the list.
			$range_out[] = $range;
			$last_num = $range;
		}
		else if(is_string($range))
		{
			// Figure out if it is just a character.
			if(strlen($range) == 1)
			{
				$range_out[] = (string)$range;
				$last_num = 0;
			}
			else
			{
				// Is probably a range of values.
				$range_exp = explode(" ", $range);
							
				if(substr($range_exp[1], 0, 1) == '-' && !is_numeric(substr($range_exp[1], 0, 1)))
				{
					// Jumping range?
					$jump = str_split($range_exp[1], 1);
					
					if(count($jump) > 0)
					{
						if($jump[1] == '%')
						{
							$step = substr($range_exp[1], 2);
						}
					}
					else
					{
						// Normal range.
						$step = 1;
					}
				}
				else
				{
					$step = 1;
				}
				
				if($range_exp[0] == '[LAST_NUM]')
				{
					$start = $last_num;
				}
				else
				{
					$exp = explode("+", $range_exp[0]);
					
					if($exp[0] == '[LAST_NUM')
					{
						$start = $last_num + trim($exp[1], ']');
					}
					else
					{
						$start = $range_exp[0];
					}
				}
				
				$end = $range_exp[2];
				
				if($start > $end)
				{
					for($i = $start; $i >= $end; $i -= $step)
					{
						$range_out[] = $i;
					}
					
					$last_num = $i;
				}
				else
				{
					for($i = $start; $i <= $end; $i += $step)
					{
						$range_out[] = $i;
					}
					
					$last_num = $i;
				}
				
				// echo $step . ", ";
			}
		}
	}
	
	if($output_type == RANGE_ARRAY)
	{
		return $range_out;
	}
	else
	{
		return implode(", ", $range_out);
	}
}

echo range_string("1, 2, 3, [LAST_NUM+1] - 6", RANGE_STRING);

?>
----
Server IP: 92.48.74.199
Probable Submitter: 78.149.119.221
----
Manual Page -- http://www.php.net/manual/en/function.range.php
Edit        -- https://master.php.net/note/edit/86037
Del: integrated  -- https://master.php.net/note/delete/86037/integrated
Del: useless     -- https://master.php.net/note/delete/86037/useless
Del: bad code    -- https://master.php.net/note/delete/86037/bad+code
Del: spam        -- https://master.php.net/note/delete/86037/spam
Del: non-english -- https://master.php.net/note/delete/86037/non-english
Del: in docs     -- https://master.php.net/note/delete/86037/in+docs
Del: other reasons-- https://master.php.net/note/delete/86037
Reject      -- https://master.php.net/note/reject/86037
Search      -- https://master.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.