Jump to content

Recommended Posts

Posted (edited)

This function parser printing-like intervals (like "1,2,3-5") and returns an array with every page number as item.

Ex.: "1,2,3-5" will return an array which 0th element is "5" (page count) and the items are 1, 2, 3, 4 and 5. The array is given in the order the $interval parameter is set, and reverse intervals are also supported (e.g.: "8,4,7-5" will return items 8, 4, 7, 6, 5 and 0th element is 5).

If it fails, it will return False and set error to non-zero (invalid string).

#cs
  Range parser
  This function parser printing-like intervals (like "1,2,3-5") and returns an array with every page number as item.
  @author Jefrey S. Santos <jefrey[at]jefrey.ml>
#ce

Func rangeparser($interval)
    $interval = StringReplace($interval, " ", "")
    If Not StringRegExp($interval, "([0-9\-\,])") Then Return SetError(1, 0, False)
    $interval = StringSplit($interval, ",")
    Dim $return[1]
    For $i = 1 To $interval[0]
        If StringInStr($interval[$i], "-") Then
            $split = StringSplit($interval[$i], "-")
            If $split[0] <> 2 Then Return SetError(1, 0, False)
            If $split[1] = $split[2] Then
                ReDim $return[UBound($return)+1]
                $return[UBound($return)-1] = $split[1]
            Else
                If $split[1] < $split[2] Then
                    $step = +1
                Else
                    $step = -1
                EndIf
                For $j = $split[1] To $split[2] Step $step
                    ReDim $return[UBound($return)+1]
                    $return[UBound($return)-1] = $j
                Next
            EndIf
        Else
            ReDim $return[UBound($return)+1]
            $return[UBound($return)-1] = $interval[$i]
        EndIf
    Next
    $return[0] = UBound($return)-1
    Return $return
EndFunc
Edited by Jefrey

My stuff

  Reveal hidden contents

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...