SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Using trim().

  1. #1
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using trim().

    I have a form which allows the user to select different categories. Each category has a specific ID number, so a typical response would be:
    "10, 7, 15, 46, 32"
    and this is stored in the variable $categories.

    So I obviously want to split this by the commas, I could use explode() or split() but for this example I'll use explode():
    $categories = explode(",", $categories);

    Now I want to get rid of the white spaces, so I'll use trim(). But, my question is, can I pass the array $categories into trim() and expect to have the white spaces stripped from each element. Or, will I have to do a while loop, extracting the white space individually from each element.

    $c = 0;
    while($categories[$c])
    {
    $categories[$c] = trim($categories[$c]);
    $c++;
    }
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here

  2. #2
    SitePoint Enthusiast drisley's Avatar
    Join Date
    Jun 2001
    Location
    Florida
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe you'll have to use the while loop. The thing to do is try it.
    David Risley
    Web Developer, Web Publisher, Blogger, Tech Dude
    PC Mechanic | WebbyOnline

  3. #3
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It worked (the while loop)... thanks!
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here

  4. #4
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can's you go:

    $categories = explode(", ", $categories);

    So the explod() gets rid of the whitespace as well?

  5. #5
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That would work fine as long as the user added a space after every comma. I thought about the setup I was running, it's much too complicated for the user to use. I'm going to change it so that an array of checkboxes is displayed, then the user will be able to check each category the image should belong to instead of remembering each Category ID.
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here


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
  •