Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jan 2013
    Posts
    74
    Thanks
    19
    Thanked 1 Time in 1 Post

    Exploding a string with a delimiter of multiple spaces

    I'm reading in information from a .txt file and I need to separate the columns based on multiple spaces - not just 1 space. The info is separated like this:

    City State Zip Code
    -------------------------------------------------
    New York NY 60111
    Chicago IL 60123


    Because the cities can have a space in them, I can't explode the string like this:

    PHP Code:
    $new_string explode(" "$old_string); 
    I also can't determine how many spaces are between the city column and the state column. So is there a way to explode the string into smaller parts based on two or more spaces separating each part of the string?

  • #2
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    924
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Maybe you could insert the states like 'New_York' then explode values to array. Once in array, search all state values for a '_' undrerscore character and then replace with a space ' ', which will give you back the original string 'New York'.

    That's what I'd do but there is most likely much better ways to do it.

    Hope it helps though.

    Regards,

    LC.
    Last edited by LearningCoder; 01-07-2013 at 09:21 PM.

  • Users who have thanked LearningCoder for this post:

    cgdtalent (01-07-2013)

  • #3
    New Coder
    Join Date
    Jan 2013
    Posts
    74
    Thanks
    19
    Thanked 1 Time in 1 Post
    Quote Originally Posted by LearningCoder View Post
    Maybe you could insert the states like 'New_York' then explode values to array. Once in array, search all state values for a '_' undrerscore character and then replace with a space ' ', which will give you back the original string 'New York'.

    That's what I'd do but there is most likely much better ways to do it.

    Hope it helps though.

    Regards,

    LC.
    I think I know what you mean but my problem is that when I read a line in from the text file, I need to separate the various columns of (state, zip, county). Because there's multiple, varying spaces separating each column, and then of course some cities have a space in them, it's not possible to explode it by a single space. I need to explode it by two spaces or more.

    Maybe you already understood that but I figured I'd clarity it a bit more.

    I just can't figure out how to explode it by two spaces or more...that's my real issue.

  • #4
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    924
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Have you tried using a double space as the delimiter, like:
    PHP Code:
    $new_string explode("  "$old_string); 
    I just created a string with a double space, used the code above and it worked fine.

    Regards,

    LC.

  • Users who have thanked LearningCoder for this post:

    cgdtalent (01-07-2013)

  • #5
    New Coder
    Join Date
    Jan 2013
    Posts
    74
    Thanks
    19
    Thanked 1 Time in 1 Post
    How about 2 or more spaces?

    The data is being read in like:
    New York   NY      60111

  • #6
    New Coder
    Join Date
    Jan 2013
    Posts
    74
    Thanks
    19
    Thanked 1 Time in 1 Post
    Thanks for taking the time to help with this but I did the easiest thing - something I should have done earlier....I just found a new zip code listing with commas separating the values instead of spaces.

  • #7
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    924
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Give this a try:
    PHP Code:
    $new_string preg_split('/ {2,}/',$old_string); 
    Regards,

    LC.


  •  

    Posting Permissions

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