Jump to content

Converting columns into substrings


TheMunky

Recommended Posts

Hey guys, I just registered a couple of minutes ago. I am pretty novice at PHP, and was wondering if you guys could help me out with this problem I have. Basically, I want to be able to convert a string that contains columns of text and make each column its own string (or put the contents into an array). Here's an example:
say I have $test = " 123 456 891
nnn jjj kkk
lol jkl lol"
I would want to put say column 1, which is 123, nnn, lol into its own string. I only know how to do this in visual basic, not php, but would it involve something like searching for spaces in each line?
Thanks in advance for any help!
Link to comment
Share on other sites

[!--quoteo(post=378929:date=Jun 1 2006, 01:26 PM:name=TheMunky)--][div class=\'quotetop\']QUOTE(TheMunky @ Jun 1 2006, 01:26 PM) [snapback]378929[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hey guys, I just registered a couple of minutes ago. I am pretty novice at PHP, and was wondering if you guys could help me out with this problem I have. Basically, I want to be able to convert a string that contains columns of text and make each column its own string (or put the contents into an array). Here's an example:
say I have $test = " 123 456 891
nnn jjj kkk
lol jkl lol"
I would want to put say column 1, which is 123, nnn, lol into its own string. I only know how to do this in visual basic, not php, but would it involve something like searching for spaces in each line?
Thanks in advance for any help!
[/quote]

$data = '123 456 891
nnn jjj kkk
lol jkl lol'

knowing how to use the wonderful 'explode' function in PHP is a must to all.

first of all, your data's column is separated by space, and the rows separate by new line:

$rows = explode("\n", $data);

now, $rows[0] is '123 456 891', $rows[1] is 'nnn jjj kkk' .. and so on.

$cols = explode(' ', $rows[0]);
then, $cols[0] will be 123, $cols[1] will be 456.. and so on.

if you put them together in a loop, you'll get what you wanted.


Tom

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.