problem using an array element and substr

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

    problem using an array element and substr

    Hi, just new to the list, but couldn't find anything about this on
    google so I hope it's never been asked before.

    I'm trying to write a function that takes a rather large (ASCII) string
    and splits it into distict sections. The input string is beyond my
    control since it will be read from a magnetic card (like a creditcard).

    The string looks like this:
    "% 999990008312?;0 008312999990000 ?"
    The important bits being the sequence of 9's and the number directly
    behind. The 9 sequence has a fixed length, the remainder isn't. My idea
    was to use explode("?", $string) to isolate the first section, strip of
    the beginning and the use substr to split the remaining string in a part
    of 5 and unknown length. I'm not the greatest coder in the world I'll
    freely admit, but this is what I came up with ($card_string being the
    string in question with the layout as above):

    $card_string = trim($card_stri ng, "%");
    $card_data = explode("?", $card_string);
    $card_ver = substr($card_da ta[0],0,5); // first 5 chars from original
    string excluding the % and all spaces
    $card_id = substr("$card_d ata[0]",6); // the rest of the string

    I expected to get $card_ver = 99999 and $card_id = 0008312, instead a
    echo or print show $card_ver = and $card_id = 999990008312.
    If I force card_data to be "9999900083 12" it does exactly that, but if I
    knew the content of the string, i wouldn't need this function so
    naturally this is not a solution.

    Am I makeing somekind of horrible mistake, is this some kind of really
    weird limitation of PHP or maybe is there a simpler way of getting this
    done anyway?

    Thanks,

    Patrick Londema

    Debian GNU/Linux Testing
    PHP Version 4.1.2
    Apache/1.3.26
    Working demo hosted on DSL line @ http://evilplatypus.net/~platypus/test/
  • Ken Robinson

    #2
    Re: problem using an array element and substr

    EvilPlatypus wrote (in part):
    [color=blue]
    > The string looks like this:
    > "% 999990008312?;0 008312999990000 ?"
    > The important bits being the sequence of 9's and the number directly
    > behind. The 9 sequence has a fixed length, the remainder isn't. My[/color]
    idea[color=blue]
    > was to use explode("?", $string) to isolate the first section, strip[/color]
    of[color=blue]
    > the beginning and the use substr to split the remaining string in a[/color]
    part[color=blue]
    > of 5 and unknown length. I'm not the greatest coder in the world I'll
    > freely admit, but this is what I came up with ($card_string being the
    > string in question with the layout as above):
    >
    > $card_string = trim($card_stri ng, "%");
    > $card_data = explode("?", $card_string);
    > $card_ver = substr($card_da ta[0],0,5); // first 5 chars from original
    > string excluding the % and all spaces
    > $card_id = substr("$card_d ata[0]",6); // the rest of the string
    >
    > I expected to get $card_ver = 99999 and $card_id = 0008312, instead a
    > echo or print show $card_ver = and $card_id = 999990008312.
    > If I force card_data to be "9999900083 12" it does exactly that, but[/color]
    if I[color=blue]
    > knew the content of the string, i wouldn't need this function so
    > naturally this is not a solution.[/color]

    Before you change any code, print out the value of $card_string after
    the trim. You will see that it is

    " 999990008312?;0 008312999990000 ?".

    Your trim statement just trimed off the "%". Change that statement to

    $card_string = trim($card_stri ng, " %");

    That should do the trick.

    Whenever you're having problems with your logic, put echo, print, or
    print_r statements after each statement that might be a problem.

    Ken

    Comment

    • Patrick Londema

      #3
      Re: problem using an array element and substr

      Ken Robinson wrote:
      [color=blue]
      > Before you change any code, print out the value of $card_string after
      > the trim. You will see that it is
      >
      > " 999990008312?;0 008312999990000 ?".
      >
      > Your trim statement just trimed off the "%". Change that statement to
      >
      > $card_string = trim($card_stri ng, " %");
      >
      > That should do the trick.
      >
      > Whenever you're having problems with your logic, put echo, print, or
      > print_r statements after each statement that might be a problem.
      >
      > Ken[/color]

      AAARGH, it's always the obvious mistakes... Thanks for the help and the
      advice!

      Patrick

      Comment

      Working...