Jump to content

Archived

This topic is now archived and is closed to further replies.

TheMunky

Converting columns into substrings

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!

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.