Order of expression in preg_split

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • none

    Order of expression in preg_split

    Hello:

    Using PHP 4.3.11

    The line: $strDate = preg_split('/[\.\/-]/', $my_date);
    works to break apart a date in the form mm/dd/yyyy or mm.dd.yyyy or
    mm-dd-yyyy
    but the line: $strDate = preg_split('/[\/-\.]/', $my_date);
    doesn't work.

    Why does the order of the items make a difference?

    Thanks,

    Ken
  • d

    #2
    Re: Order of expression in preg_split

    "none" <""krwl\"@(none )"> wrote in message
    news:ML2dnQej5s hzZG7eRVn-iA@rcn.net...[color=blue]
    > Hello:
    >
    > Using PHP 4.3.11
    >
    > The line: $strDate = preg_split('/[\.\/-]/', $my_date);
    > works to break apart a date in the form mm/dd/yyyy or mm.dd.yyyy or
    > mm-dd-yyyy
    > but the line: $strDate = preg_split('/[\/-\.]/', $my_date);
    > doesn't work.
    >
    > Why does the order of the items make a difference?[/color]

    In a regular expression character class definition (the bit in the []s), a
    hyphen denotes a range of characters, so [a-z] will match all lowercase
    letters of the alphabet, and [0-9] will match all numbers from 0 to 9. If
    you escape your hyphen, it doesn't matter where you put it in your class.

    $strDate = preg_split('/[\/\-\.]/', $my_date);
    [color=blue]
    > Thanks,
    >
    > Ken[/color]

    hope that helps!

    dave


    Comment

    • Al13n

      #3
      Re: Order of expression in preg_split

      "d" <d@example.co m> wrote:[color=blue]
      >In a regular expression character class definition (the bit in the []s), a
      >hyphen denotes a range of characters, so [a-z] will match all lowercase
      >letters of the alphabet, and [0-9] will match all numbers from 0 to 9. If
      >you escape your hyphen, it doesn't matter where you put it in your class.
      >
      >$strDate = preg_split('/[\/\-\.]/', $my_date);[/color]

      Absolutely right. Also, this is rather trivial, but you shouldn't need to
      escape the . in a character class, it's only a token when used outside a
      character class. When inside a character class, the dot as well as
      quantifiers (like *, ? etc.) are interpreted as literals.

      Allen

      Comment

      • none

        #4
        Re: Order of expression in preg_split

        Al13n wrote:[color=blue][color=green]
        >>In a regular expression character class definition (the bit in the []s), a
        >>hyphen denotes a range of characters, so [a-z] will match all lowercase
        >>letters of the alphabet, and [0-9] will match all numbers from 0 to 9. If
        >>you escape your hyphen, it doesn't matter where you put it in your class.
        >>
        >>$strDate = preg_split('/[\/\-\.]/', $my_date);[/color]
        > Absolutely right. Also, this is rather trivial, but you shouldn't need to
        > escape the . in a character class, it's only a token when used outside a
        > character class. When inside a character class, the dot as well as
        > quantifiers (like *, ? etc.) are interpreted as literals.[/color]

        Allen and Dave:

        Thanks very much for your help. I'm surprised that /[\.\/-]/ worked at
        all! A better way to have written it would have been: #[./\-]# which
        works great.

        Thanks, again.

        Ken

        Comment

        Working...