Jump to content


Photo

Converting columns into substrings


  • Please log in to reply
2 replies to this topic

#1 TheMunky

TheMunky
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 01 June 2006 - 03:26 AM

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!

#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 01 June 2006 - 04:23 AM

[!--quoteo(post=378929:date=Jun 1 2006, 01:26 PM:name=TheMunky)--][div class=\'quotetop\']QUOTE(TheMunky @ Jun 1 2006, 01:26 PM) View Post[/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


Life's too short for arguing.

#3 TheMunky

TheMunky
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 01 June 2006 - 11:38 PM

Figured it out, thanks for all the help!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users