space delimiters

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dannyd
Forum Commoner
Posts: 56
Joined: Wed Jan 23, 2008 11:31 am

space delimiters

Post by dannyd »

Sorry to post again but now when I tried searching in google for a string on how figure out my issue this thread was the second listed choice. I'm really not able to find an answer on how to parse a space separated file that has variable spaces between the columns.

If excel is able to import the file successfully by selecting the space delimiter. And selecting " " as the delimiter doesnt work in PHP would this be a case where I have to use rtrim ?

I have no idea. Any suggestions ?

I even tried adding commas between the columns and using a comma as the delimiter in the code and it worked fine, so it must be an issue with the length of spaces between columns.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: space delimiters

Post by liljester »

Im not sure what youre trying to do, but, you can read the file 1 line at a time and use preg_split() to split the line into columns in an array. the pattern "/[\s]+/" matches 1 or more spaces, and PREG_SPLIT_NO_EMPTY tells the function to not capture any empty strings.

Code: Select all

preg_split("/[\s]+/", $file_content, -1, PREG_SPLIT_NO_EMPTY);

hope that helps.
Post Reply