Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-07-2013, 09:14 PM   PM User | #1
cgdtalent
New Coder

 
Join Date: Jan 2013
Posts: 35
Thanks: 11
Thanked 1 Time in 1 Post
cgdtalent is an unknown quantity at this point
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?
cgdtalent is offline   Reply With Quote
Old 01-07-2013, 09:19 PM   PM User | #2
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
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..
LearningCoder is offline   Reply With Quote
Users who have thanked LearningCoder for this post:
cgdtalent (01-07-2013)
Old 01-07-2013, 09:34 PM   PM User | #3
cgdtalent
New Coder

 
Join Date: Jan 2013
Posts: 35
Thanks: 11
Thanked 1 Time in 1 Post
cgdtalent is an unknown quantity at this point
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.
cgdtalent is offline   Reply With Quote
Old 01-07-2013, 09:42 PM   PM User | #4
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
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.
LearningCoder is offline   Reply With Quote
Users who have thanked LearningCoder for this post:
cgdtalent (01-07-2013)
Old 01-07-2013, 10:11 PM   PM User | #5
cgdtalent
New Coder

 
Join Date: Jan 2013
Posts: 35
Thanks: 11
Thanked 1 Time in 1 Post
cgdtalent is an unknown quantity at this point
How about 2 or more spaces?

The data is being read in like:
New York   NY      60111
cgdtalent is offline   Reply With Quote
Old 01-07-2013, 10:18 PM   PM User | #6
cgdtalent
New Coder

 
Join Date: Jan 2013
Posts: 35
Thanks: 11
Thanked 1 Time in 1 Post
cgdtalent is an unknown quantity at this point
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.
cgdtalent is offline   Reply With Quote
Old 01-07-2013, 10:27 PM   PM User | #7
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Give this a try:
PHP Code:
$new_string preg_split('/ {2,}/',$old_string); 
Regards,

LC.
LearningCoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:59 PM.


Advertisement
Log in to turn off these ads.