SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Parsing Strings

Hybrid View

  1. #1
    You want what? By when?? Milamber's Avatar
    Join Date
    Jan 2001
    Location
    California
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hulo.

    I am (confused).

    I know this should be a very simple thing to do, but I just cant figure the logistics out of how to do it. That and I KNOW it is sitting there staring at me.

    Here;s the deal, When a user submits a form it will either have a single entry in a certain field, or multiple entries separated by "," in it. I need a routine that will take the entry and return either a list of the entries and how many there are, OR the single entry.

    IE:

    i get this: "thing1"

    and then the routine returns $things witht the value "thing1".

    But if i get this: "thing1, thing2, thing3"

    the routine returns this:

    $things[0] = thing1
    $things[1] = thing2
    $things[2] = thing3

    How do i do this?

    Thanks,
    Mil

  2. #2
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this piece of code: assume you're using PHP
    Code:
    $things = explode(",",$input);
    for ($i=0;$i<count($things);$i++)
    { echo("things[$i] = $thing[$i] ");
    }
    If the input only has 1 element, the explode will return a one entry-array, which is also appropriate using the same loop. Or if you want if there is one entry, evaluate the array as a scalar?
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  3. #3
    You want what? By when?? Milamber's Avatar
    Join Date
    Jan 2001
    Location
    California
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great! I knew it could be done easily. That code will work great, however, just one last quick little thing, what would i do to the string before i exploded it to get rid of ALL the white space in it.

    trim() gets the stuff at the beginning and end, but I want to be rid of it all.

    Thanks mucho!
    -Mil

  4. #4
    You want what? By when?? Milamber's Avatar
    Join Date
    Jan 2001
    Location
    California
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, what is this scalar thing your talking about?

    -Mil

  5. #5
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to remove all spaces, use:
    $output = str_replace(" ","",$output);

    And the scalar is the simplest type, $a = 1; => $a is a scalar.
    And next is array
    next is hash (key-value array)
    and more.
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •